I have a large number of key dictionaries inside a dictionary named data. I would like to iterate over the collection of dicts to find the bus of type 12, which must be only one in the collection. I came up with the following piece of lines, but is there any improvement for this to save time?
for i = 1:length(data["bus"])
if data["bus"]["$(i)"]["type"] == 12
global new_key = data["bus"]["$(i)"]["key"];
Is this a homework problem?
In a real-life application, I would consider the following first:
- choose another datastructure for buses, eg a
struct or a
- don’t convert to strings for keys, use
- wrap the whole thing in a function, and just pass
data["bus"] to it,
- think about returning the whole bus datastructure instead of
key (but this depends on the context)
- use a standard idiom like
filter for getting buses of a given type (are they unique by type?)
See the manual for all of these.
An alternative is just dumping everything in a dataframe using a “tidy” structure. See
No, it is not a homework, but I am just working on a project and would like to improve it.
findall work in this case?
for starter I guess, notice you’re reading
data["bus"]["$(i)"] twice (I assume)
yeah, the first one is for checking condition, and the other is for assigning if condition is met.