How to improve this code

Hi,

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"];
    end
end

Thank you

Is this a homework problem?

In a real-life application, I would consider the following first:

  1. choose another datastructure for buses, eg a struct or a NamedTuple,
  2. don’t convert to strings for keys, use i directly,
  3. wrap the whole thing in a function, and just pass data["bus"] to it,
  4. think about returning the whole bus datastructure instead of key (but this depends on the context)
  5. use a standard idiom like findall or 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

https://juliadata.github.io/DataFrames.jl/stable/

3 Likes

Hi @Tamas_Papp,

No, it is not a homework, but I am just working on a project and would like to improve it.

How can findall work in this case?

Thank you!

for starter I guess, notice you’re reading data["bus"]["$(i)"] twice (I assume)

1 Like

yeah, the first one is for checking condition, and the other is for assigning if condition is met.