Sorry for the cryptic notation. I can walk you through it a bit. First, I looked at your data, which is just a list of lists. You indicated that there is a bit of implicit structure: the first element of each "row" is a model number (i.e. some sort of key) and the other elements are parameters (i.e. just data associated to the model number).
{{4154, 0.4, 0.5, 0.01, 0.65}, {4144, 0.3, 0.6, 0.11, 0.54}, {4146,
0.2, 0.7, 0.21, 0.63}, {4147, 0.1, 0.8, 0.31, 0.62}, {4148, 0.11,
0.9, 0.41, 0.61}}
An Association is just a list of key-value pairs (similar to but much more flexible than hash maps or dictionaries in other languages). The representation of a single key-value pair uses a structure called a Rule. You can create a Rule like this: Rule[x,y]
, but there is a nice shortcut: x->y
. Associations are basically just a list of such Rules. So, what I wanted (in my original implementation) is a list that looks like this:
{4154 -> {0.4, 0.5, 0.01, 0.65}, 4144 -> {0.3, 0.6, 0.11, 0.54},
4146 -> {0.2, 0.7, 0.21, 0.63}, 4147 -> {0.1, 0.8, 0.31, 0.62},
4148 -> {0.11, 0.9, 0.41, 0.61}}
Then I just wrapped it up in an Association,
modelNumberData=Association[...the list goes here...]
which gives
<|4154 -> {0.4, 0.5, 0.01, 0.65}, 4144 -> {0.3, 0.6, 0.11, 0.54},
4146 -> {0.2, 0.7, 0.21, 0.63}, 4147 -> {0.1, 0.8, 0.31, 0.62},
4148 -> {0.11, 0.9, 0.41, 0.61}|>
and bingo, we can access stuff with a key, like this:
modelNumberData[4154]
which gives
{0.4, 0.5, 0.01, 0.65}
The <|
and |>
are just shorthand for Association.
But I don't want to type in the list ("...the list goes here..." bit) by hand. What I want is to split each "row" into a pair whose first element is the first of the pair and whose "tail" is a list that will be the second element of the pair. And by "pair", I really mean Rule. Mathematica has the functions that I need built in: First and Rest. So, for each row, I want:
Rule[First[row],Rest[row]]
Mathematica also has a function called Map that applies a given function to every element of a list. Well, each row of your data is an element of a list, so if I can just turn that Rule expression into a function, I can Map it over your data. Well, Mathematica has many ways to specify a function, and one is Function (I know, such crazy names, eh?). You can use Function with named arguments, or with positional arguments (look up Slot in the documentation), and I chose positional arguments. Well, I only need one argument, so the positional aspect isn't really apparent, but anyway, it looks like this:
Function[Rule[First[#], Rest[#]]]
Putting this all together, I was able to restructure your data by evaluating this:
Map[Function[Rule[First[#], Rest[#]]], data]
Hmm. That doesn't look like what I originally posted. Well, there are nice shortcuts and syntactic sugar that shorten things. The arrow, ->
is infix notation for Rule, so a->b
is the same as Rule[a,b]
. So, we can simplify our Function: Function[First@# -> Rest@#]
. The ampersand is postfix notation for Function, so we can reduce further: (First@# -> Rest@#) &
. The parentheses aren't necessary, but they can sometimes help you keep track of things. Finally, Map also has an infix notation: /@
, and so we finally arrive at (First@# -> Rest@#) & /@ data
.