Message Boards Message Boards

0
|
8575 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

operations with lists

Posted 10 years ago
Hi all, I turn to you to ask for your help with the following problem, I have the following list:
{{2,15,22}, {4,13,27}, {3,17,29}, {5,18,29}, {3,11,25}}
and what I try to do is THE FOLLOWING:
{{15,22},{15,22},{13,27},{13,27},{13,27},{13,27},{17,29},{17,29},{17,29},{18,29},{18,29},{18,29},{18,29},{18,29},{11,25},{11,25},{11,25}}
as will realize it is not very common but I think someone may help.
 following with list, if I have the next list
{{15,22}, {27,13}, {17,29}, {29,16}, {11,25}}
ordering follows if the second element of each list is smaller than the first, then make an exchange of the second change to the first, in our case the next element is the first greater{27,13} should be  {13,27} and also have another element with the same problem {29,16}, should be {16,29}
any idea is welcome.
POSTED BY: Luis Ledesma
3 Replies
Posted 10 years ago
 (*Let's say this is your list*)
 list = {{2, 15, 22}, {4, 13, 27}, {3, 17, 29}, {5, 18, 29}, {3, 11, 25}, {2, 17, 11}, {2, 24, 13}};
 
 In[2]:= Flatten[
  Cases[list, {a_, b_, c_} :>
    If[b < c, Table[{b, c}, {a}], Table[{c, b}, {a}]]], 1]
 
 Out[2]= {{15, 22}, {15, 22}, {13, 27}, {13, 27}, {13, 27}, {13, 27}, {17,
   29}, {17, 29}, {17, 29}, {18, 29}, {18, 29}, {18, 29}, {18,
  29}, {18, 29}, {11, 25}, {11, 25}, {11, 25}, {11, 17}, {11,
  17}, {13, 24}, {13, 24}}
Or you can also write:
In[3]:= Flatten[Cases[list, {a_, b_, c_} :> Table[{b, c}, {a}]], 
  1] /. {x_, y_} /; x > y -> {y, x}


Out[3]= {{15, 22}, {15, 22}, {13, 27}, {13, 27}, {13, 27}, {13, 
  27}, {17, 29}, {17, 29}, {17, 29}, {18, 29}, {18, 29}, {18, 
  29}, {18, 29}, {18, 29}, {11, 25}, {11, 25}, {11, 25}, {11, 
  17}, {11, 17}, {13, 24}, {13, 24}}
POSTED BY: Girish Arabale
Posted 10 years ago
Many thanks Terrence, it works very well, your help is unvaluable
POSTED BY: Luis Ledesma
f1[L_List] := Flatten[Table[Rest[#], {First[#]}] & /@ L, 1]
f2[L_List] := Sort /@ L
I think this is all you're looking for.  There are probably slicker solutions.
POSTED BY: Terrence Honan
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract