5
$\begingroup$

Given the following very simple example:

list1 = {a11, a12, a13, a14, a15};
list2 = {a21, a22, a23, a24, a25};
list3 = {a31, a32, a33, a34, a35};
biglist = {list1, list2, list3};

are the names "list1", "list2", "list3" of the sub-lists "lost" inside the "biglist"? For example, if I need to know only which sub-lists are included in the "biglist" (by name), is this possible? Further, can I get, for example, the second element of all sub-lists, can I use something like newlista = biglist[[list3, [[2]]]] instead of newlist = biglist[[3,2]] ? This would be useful, for example, in case I don't know the order of the sub-lists inside the "biglist".

$\endgroup$
  • 2
    $\begingroup$ What version do you use? Associations are the way to go if 10+. $\endgroup$ – Kuba Feb 22 '17 at 15:07
  • 1
    $\begingroup$ I have version 11, so it should be available. Thanks, I'll test it right now! It's difficult for me to search for functions which were new added to Mathematica: there are hundreds of them :-). $\endgroup$ – Conrad Feb 22 '17 at 15:12
  • $\begingroup$ No problem. p.s. about your question, does the accepted answer in ".. list of assigned variables" answer your doubts? $\endgroup$ – Kuba Feb 22 '17 at 15:14
  • $\begingroup$ Yes, in a way... $\endgroup$ – Conrad Feb 22 '17 at 16:07
  • 1
    $\begingroup$ oh, then I was thinking about something like biglist = <|"list1" -> list1,...|> $\endgroup$ – Kuba Feb 22 '17 at 16:44
3
$\begingroup$

Elucidating Kuba's comments, here's how you can use Association to accomplish your goals.

list1 = {a11, a12, a13, a14, a15};
list2 = {a21, a22, a23, a24, a25};
list3 = {a31, a32, a33, a34, a35};

lists = <|"list1" -> list1, "list2" -> list2, "list3" -> list3|>;

(After lists is defined list1 etc. may be cleared.)

I need to know only which sub-lists are included in the "biglist" (by name)

Keys[lists]
{"list1", "list2", "list3"}

can I get, for example, the second element of all sub-lists

lists[[All, 2]] // Values
{a12, a22, a32}

can I use something like newlista = biglist[[list3, [[2]]]] ?

lists[["list3", 4]]
a34

for example, in case I don't know the order of the sub-lists inside the "biglist"

(* pick one *)

{"list2", "list1"} /. lists

lists /@ {"list2", "list1"}

Lookup[lists, {"list2", "list1"}]
{{a21, a22, a23, a24, a25}, {a11, a12, a13, a14, a15}}
$\endgroup$
  • $\begingroup$ Thanks a lot for your very precise and detailed answer! $\endgroup$ – Conrad Feb 23 '17 at 6:42
  • $\begingroup$ Works perfect, it's a very logical way to do it and it's exactly what I was looking for! $\endgroup$ – Conrad Feb 23 '17 at 6:51
3
$\begingroup$

I made a simple functions which - I think - could help:

l1 = {"a", "b", "c", "d"};
l2 = {"e", "f", "g"};
l3 = {"h", "i", "j", "k", "l"};
l = {l1, l2, l3};

In[40]:= l[[3, 2]]

Out[40]= "i"

In[50]:= picker[a_?ListQ, x_?ListQ, y_?NumberQ] := 
 If[SubsetQ[Flatten[a], x], x[[y]]]

In[52]:= picker[l, l3, 2]

Out[52]= i
$\endgroup$
  • 1
    $\begingroup$ Also a useful approach, thanks. $\endgroup$ – Conrad Feb 23 '17 at 6:43

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.