Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a group of lists, for example:

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

Now, I want to multy-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 multy set then?

share|improve this question

4 Answers 4

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

share|improve this answer
    
How about lsX but not ls[X]? –  bushiwo Oct 25 '13 at 3:27
    
@bushiwo. You're better off doing it this way in Mathematica. Unless there's a specific reason you want it that way. –  RunnyKine Oct 25 '13 at 3:29
1  
@bushiwo. See my edit. That's what you want right? –  RunnyKine Oct 25 '13 at 3:35
    
Could you tell me why I can't use my names function in my UPDATE? –  bushiwo Oct 25 '13 at 3:43
    
@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? –  RunnyKine Oct 25 '13 at 3:48

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
*)
share|improve this answer

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]
share|improve this answer
    
I was referring to MapIndexed[(all[#2[[1]]] = #1) &, lists] –  belisarius Oct 25 '13 at 3:48
    
Very nice. I guess this means that I was reinventing MapIndexed -- don't think I've ever used it before. –  bill s Oct 25 '13 at 4:01

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}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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