1
$\begingroup$

I have a list as

list={-3/4, -3/4, 3/4, 3/4, -1/4, -1/4, -1/4, -1/4, -1/4, -1/4,
        1/4, 1/4, 1/4, 1/4, 1/4, 1/4}

I want to create pairs with an added selected number in Range[3] in this method:

1-deleting same elements,
2-sorting by last,
3-adding and creating pairs and
4-specifying a name to the list

I use of Sort@Partition[list, 1] and after that use of Appendto but it doesn't work correctly, also the specifying a name for created data is more difficult!)

data1={{1,-3/4},{1, -1/4},{1, 1/4},{1,3/4}}
data2={{2,-3/4},{2, -1/4},{2, 1/4},{2,3/4}}
data3={{3,-3/4},{3, -1/4},{3, 1/4},{3,3/4}}
$\endgroup$
2
  • $\begingroup$ Using data[ i ] is easier and more natural. Why do you want datai? $\endgroup$ Sep 11, 2015 at 12:19
  • $\begingroup$ yes you are right, I can use of that $\endgroup$ Sep 11, 2015 at 12:30

2 Answers 2

5
$\begingroup$
o = Outer[List, Range@3, Union@list];
data[i_] := o[[i]]

data /@ Range@3

(*
{{{1, -(3/4)}, {1, -(1/4)}, {1, 1/4}, {1, 3/4}},
 {{2, -(3/4)}, {2, -(1/4)}, {2, 1/4}, {2, 3/4}}, 
 {{3, -(3/4)}, {3, -(1/4)}, {3, 1/4}, {3, 3/4}}}
*)
$\endgroup$
1
$\begingroup$

You could also just define data:

data[n_] := Thread[{n, Union@list}]

You could also use Tuples for a belisarius variant:

tup=GatherBy[Tuples[{Range@3, Union@list}], First];
dat[j_]:=tup[[j]]

but Outer seems neater for the original post.

$\endgroup$
3
  • $\begingroup$ Please try list = RandomInteger[{1, 1000}, 1000000]; data[n_] := Thread[{n, Union@list}]; data[1]; // Timing $\endgroup$ Sep 11, 2015 at 14:57
  • $\begingroup$ @belisarius thank you for the instructive comment. I did not know what the size of the problem this was going to be applied to. In retrospect, I should have more thoughtful and less tempted by terse looking. Always learning...:) $\endgroup$
    – ubpdqn
    Sep 11, 2015 at 22:23
  • $\begingroup$ He! Yours was my first try! :) $\endgroup$ Sep 11, 2015 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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