3
$\begingroup$

Trying to play around with Wolfram Mathematica to manipulate data as dictionaries - associative arrays.

http://reference.wolfram.com/language/ref/Association.html

I am looking at examples, but cannot find either in Google a simple example to show how to import data from a CSV and map it into a dictionary key value.

Example, I want to map:

data = {{"7A37D069", "Copper"}, {"6D2C07C7", "Autograph"}}

Into a dictionary, with first element of tuples as key, the others as values.

I want to call an item of the dictionary with something like:

data['7A37D069'] #python syntax, don't know the Wolfram Math one

I tried with:

Map[data, <|data[[All,1]]-->data[[All,2]]|>]

and

Map[data, <|a --> x|]

Could you help me understand the syntax to map a function over data?

I want a dictionary as final result.

$\endgroup$
3
$\begingroup$

Like this?

AssociationThread @@ Transpose@data
(*  <|"7A37D069" -> "Copper", "6D2C07C7" -> "Autograph"|>  *)

The data in the comment below can be made into an Association as follows:

AssociationThread[data[[All, 1]], data[[All, 2 ;;]]]
(*
  <|"7A37D069" -> {"Copper"},
    "6D2C07C7" -> {"Autograph"},  
    "848E0A46" -> {"route", "PIC microcontroller", "Composition", 
         "Knight", "Glacier", "Atlas", "Region", "Vision"}|>
*)
$\endgroup$
  • $\begingroup$ Could you explain what it does and make the association generally applicable? Given a list of list: data = {{"7A37D069", "Copper"}, {"6D2C07C7", "Autograph"}, {"848E0A46", "route", "PIC microcontroller", "Composition", "Knight", "Glacier", "Atlas", "Region", "Vision"}} I would like to make a dictionary where keys are the first element of tuples, and values all the others. How to generalise the "first element" and "other element" syntax, with no need to explicitly write the values as "7A37D069" -> "Copper" $\endgroup$ – user305883 Nov 6 '16 at 0:14
  • 1
    $\begingroup$ @user305883 Your example data had the form {{key1, val1}, {key2, val2}}. Transpose transposes the array (like a matrix) to {{key1, key2}, {val1, val2}} or generally {keys, vals}. AssociationThread @@ {k, v} first becomes Association[k, v], which then creates an Association as described in the documentation. I assumed you know what an Association does and how it can act as a dictionary, since you use it in your question. The data in your comment has quite a different structure than the data in the question.... $\endgroup$ – Michael E2 Nov 6 '16 at 0:21
  • $\begingroup$ Thank you for the explanation. Your answer is correct for me, but I am still missing a way to map a function onto a dataset. Do you have to make explicit all the key value pairs in the (* *) syntax? I import a csv file with thousands of rows, I want to use the first column as key. What is a compact way to create an Association passing data itself into (* *) syntax, considering that all first elements of tuples (or CSV column) are always keys? $\endgroup$ – user305883 Nov 6 '16 at 11:12
  • $\begingroup$ @user305883 Maybe one of us is confused: The code between the comments (* *) is the output of the line immediately above it. If csvdata is the imported data, then csvdata[[All, 1]] would be column 1, and csvdata[[All, 2 ;;]] would be columns 2, 3, etc. You can create the dictionary/Association with dict = AssociationThread[csvdata[[All, 1]], csvdata[[All, 2 ;;]]]. Isn't that what you want? $\endgroup$ – Michael E2 Nov 6 '16 at 11:20
  • $\begingroup$ :P me confused - thank you, I thought it was syntax for Association <|...|> and forgot (* *) was to comment out in Mathematica $\endgroup$ – user305883 Nov 6 '16 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.