Setting up Dict example puzzled by syntax and functionality

Hi all
noob here. I am SLOWLY learning how to code properly in julia and trying NOT to bring my python baggage with me. I’ve set myself a high level goal and sub divided it into tasks. So I wanted to read a csv and put it into a Dict. I read up on Dicts, pretty standard stuff, and then thought to look for an example to reverse engineer. I found one and it contains the following code

the csv file

state,abbrev,fips
alabama,AL,01
alaska,AK,02
arizona,AZ,04

the code I found.

states_dict = Dict(
    "headers" => [Dict("text" => "Name", "value" => "name"), Dict("text" => "Abbreviation", "value" => "abbrev"), Dict("text" => "FIPS", "value" => "fips")],
    "states" => [Dict("name" => states[1][i,1], "abbrev" => states[1][i,2], "fips" => states[1][i,3]) for i in 1:size(states[1],1)]
)

I can see what it does but am confused about HOW to track down some aspects of the solution.

I don’t understand the relationship of headers “name” to states “name” in this case?

AND

how to track down how this works. I can see WHAT it does but can’t find anywhere that explains how “text” is defined. vscode gave me nothing.

"text" => "Name",
 [Dict("name" => states[1][i,1]

this looks highly contrived, just use CSV.jl.

It looks like states[1] is a N x 3 matrix, so on each row (ith row), you have 3 elements, corresponding to what you see in the CSV


"text" is a literal string, it doesn’t need to defined before this because it’s not a variable.

3 Likes

Hi there,

I get that bit, it’s just a comprehension and makes sense to me. It’s the relationship of “name” from headers to the “name” in states. Again, I can see what it does but not how it does it.

Why Dicts? The code you posted looks pretty nonsensical to me tbh.

What’s wrong with CSV.read(“mycsv.csv", DataFrame) ?

1 Like

Hi @nilshg and @jling hope your weekend is going well. I am trying out new things and setting myself tasks. I wanted to see how to put a csv into a Dict to get a better understanding of how Dict works should I need to use it later on.

this is NOT my code I found it on discourse.

there’s nothing wrong with CSV,jl and I do understand how to use it thanks to you good people BUT I wanted to look at this approach to learn.

Quite frankly with a bit of background knowledge about your wider goal I don’t think you should bother trying to understand how to wrangle data with Dicts.

Dicts can of course be a very useful structure at times, but they are nowhere near as important and ubiquitous in Julia as they are in Python.

2 Likes

It’s not the array that I am confused about it’s “names” how it relates to headers names.

So how can I research the relationship of “text” => “Name” I can figure out what it’s doing but would like to read more. Any ideas where to look please?

https://docs.julialang.org/en/v1/base/collections/#Core.Pair
https://docs.julialang.org/en/v1/base/collections/#Base.Dict

1 Like

Honestly I don’t think there’s much to learn here - the code you’re showing just seems to be (as Jerry says) an insanely contrived way of storing some very simple information in a very complex deeply nested structure (a Dict where each value is a vector of Dicts for a simple table?!?).

2 Likes

I hear what you are saying but I think there might be a case for Dicts in my approach. I would like to put this query to bed as it’s bugging me :wink:

Hi @nilshg and @jling
I hear you both and respect your opinion. I’ll back off from trying to understand what is going on here. One thought about that, I think I might have taken the example out of context so it might be the BEST solution to a problem I didn’t include in my question.

Here is the place I found it. the example I was looking at

thanks for giving me your counsel

This is exactly right. That issue is dealing with how to split/store the data (arbitrary data, actually) in a way that is conducive to displaying it as a table using HTML.

I’m not sure which sections you do or don’t understand, but I hope it’s been clear that this isn’t an approach you want to take if you’re actually doing data analysis of any sort.

In any case:

states_dict["headers"] tells you what the keys of each sub-dictionary are, and also gives a “nicer” name to print out as the table header for the column corresponding to that key. So the value under states_dict["states"][1]["name"] should print under the header "Name" (because that’s the value that goes with "name" in the headers dict). If that sounds convoluted, it’s because it is!

As a side note in general, to figure out what code snippets like this do (and how) I suggest working in the REPL. Copy paste that code then play around with states_dict result until you have a feel for how it works.

2 Likes

hi @tomerarnon thank you for taking the time to add your counsel. I am learning a LOT from the good people on discourse. I wanted to make sure that the original thread was cited as I thought I had made an error not linking it in the first place. @nilshg and @jling are being VERY patient with me and I do value their opinion.

What is STILL confusing to me is what the code

"text" => "Name"

actually does. I read the docs that @jling was kind enough to link to ( which I had read before) but I STILL don’t see it. I agree that I should use the REPL more as vscode is not really my cup of tea. I prefer less noise which is what the REPL does. thanks again

it doesn’t do anything, for whatever weird reason,

"headers" => [Dict("text" => "Name", "value" => "name"), Dict("text" => "Abbreviation", "value" => "abbrev"), Dict("text" => "FIPS", "value" => "fips")]

this is a Pair{String, Vector{Dict{String, String}}}.

The "headers" is a String, and the [] is a Vector of Dictionary

don’t do this, don’t use this structure for this data, just don’t.



julia> a = 2 => 3;

julia> typeof(a)
Pair{Int64, Int64}

julia> b = (2, 3)
(2, 3)

julia> typeof(b)
Tuple{Int64, Int64}

@jling not sure what you mean by

don’t do this, don’t use this structure for this data, just don’t.

It is unclear to me :slight_smile: just kidding. It made me laugh out loud. Thanks so much for taking the time to revisit this. I have learnt a LOT from this exchange and thanks to all of you for spending time on a Sunday to help out a poor noob.

The code that you are referencing has a very specific purpose - to wrangle CSV data into a very specific format using Julia Dicts, so that it can then be written to a very specific JSON format that is expected by a Vuetify data table. Its sole purpose is to allow you to display a nice, interactive data table inside of a Pluto notebook and it’s certainly not something you would typically do…in other words, it’s about the worst example you could have found if you are trying to understand Julia Dicts and how one typically uses them…to be fair though, I think if you read from the beginning of that thread, the purpose of that piece of code is quite clear…

2 Likes

Hi @mthelm85 this is completely my fault and I took your code out of context and used it incorrectly. I apologize unreservedly. I jumped into your original thread without reading it properly and, obviously, reaped the benefits of my laziness. I’m taking @nilshg, @jling and @tomerarnon collective advise and going back to the drawing board. I thought there were many interesting things in the code that I didn’t understand and thought I would learn from them. Sorry again
theakson