0
$\begingroup$

I have a group of lists, for example:

lists = RandomInteger[10, {100, 2}]
{{6, 7}, {7, 4}, {3, 3}, ....}

Now, I want to multi-set as follow:

ls1={6,7};
ls2={7,4};
,,,,
lsX=lists[[X]]

How to do that by a simple way?

Thanks.


UPDATE:

How about lsX but not ls[X]?

I have tried to generate a list of names:

names=ToExpression["ls" <> ToString@# & /@ Range[100]]

How to multi set then?

$\endgroup$
2
$\begingroup$
lists = RandomInteger[10, {100, 2}];       
Table[ls[i] = lists[[i]], {i, 1, Length @ lists}];

OR

Do[ls[i] = lists[[i]], {i, 1, Length @ lists}];

OR

For[i = 1, i <= Length @ lists, i++, ls[i] = lists[[i]]]

Now you can individually get their values e.g ls[1], ls[2] etc.

EDIT

If you really want ls1, ls2 ... then the following should do it:

Table[Evaluate[Symbol["ls" <> ToString[i]]] = lists[[i]], {i, 1, Length @ lists}];

Now you can use ls1 , ls2, etc

$\endgroup$
  • $\begingroup$ How about lsX but not ls[X]? $\endgroup$ – bushiwo Oct 25 '13 at 3:27
  • $\begingroup$ @bushiwo. You're better off doing it this way in Mathematica. Unless there's a specific reason you want it that way. $\endgroup$ – RunnyKine Oct 25 '13 at 3:29
  • 1
    $\begingroup$ @bushiwo. See my edit. That's what you want right? $\endgroup$ – RunnyKine Oct 25 '13 at 3:35
  • $\begingroup$ Could you tell me why I can't use my names function in my UPDATE? $\endgroup$ – bushiwo Oct 25 '13 at 3:43
  • $\begingroup$ @bushiwo. All you've done is create a bunch of Mathematica expressions (in this case symbols) using ToExpression. Now you have to assign them values individually, which defeats the purpose. In my updated answer, I create and assign the values at once. Is that not what you want? $\endgroup$ – RunnyKine Oct 25 '13 at 3:48
2
$\begingroup$

Not that I think this particularly useful, but it does exactly what you asked for:

lists = RandomInteger[10, {5, 2}]; 

MapIndexed[(Evaluate[Symbol["ls" <> ToString[#2[[1]]]]] = #1) &, lists];

{ls1, ls2, ls3, ls4, ls5} == lists
(*
True
*)
$\endgroup$
2
$\begingroup$

I believe this will achieve your aim. I have used "l" rather than "ls" but you can change.

MapThread[
 Set[#1, #2] &, {Symbol /@ (Table["l" <> ToString[j], {j, 100}]), 
  lists}]
$\endgroup$
1
$\begingroup$

What about this?

Set @@@ Transpose[{names, lists}]
$\endgroup$
0
$\begingroup$

This is a reasonable application of Map, which uses the shortcut /@

lists = RandomInteger[10, {100, 2}]; 
(ls[#] = lists[[#]]) & /@ Range[Length[lists]]

As requested, ls[1] is the first of the elements in list, ls[2] the second, etc. As suggested by Belisarius, this can also be done more succinctly using MapIndexed

MapIndexed[(all[#2[[1]]] = #1) &, lists]
$\endgroup$
  • $\begingroup$ I was referring to MapIndexed[(all[#2[[1]]] = #1) &, lists] $\endgroup$ – Dr. belisarius Oct 25 '13 at 3:48
  • $\begingroup$ Very nice. I guess this means that I was reinventing MapIndexed -- don't think I've ever used it before. $\endgroup$ – bill s Oct 25 '13 at 4:01

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.