Sort a List, in a List of Lists of Lists

Sort a List, in a List of Lists of Lists

Post by leigh.pasc » Sun, 14 Nov 2010 14:59:30


Dear Mathgroup,

I have a lsit of Lists of Lists:

{{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}}

I would like to sort the elements in the lowest level of brackets to give

{{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}}

i.e retaining the same structure with the paired elements in the
original order. I can't seem to get the syntax right to do this apart
from the obvious

{{Sort[{1, 2}], Sort[{2, 1}], Sort[{1, 1}]}, {Sort[{1, 1}],
Sort[{1, 1}], Sort[{1, 2}]}, {Sort[{2, 1}], Sort[{2, 2}],
Sort[{1, 2}]}, {Sort[{2, 2}], Sort[{1, 2}],
Sort[{2, 2}]}, {Sort[{1, 1}], Sort[{2, 1}],
Sort[{1, 2}]}, {Sort[{1, 2}], Sort[{2, 2}], Sort[{2, 2}]}}

I must have a blind spot for the correct command. Can someone please
help me with what should be a straightforward sort. As I want to carry
this out on several hundred thousand pairs I need a more efficient
command. Thanks in advance.

LP

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
 
 
 

Sort a List, in a List of Lists of Lists

Post by Raff » Mon, 15 Nov 2010 20:05:50


lists = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2, 1},
{2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2, 1}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}};

If the depth is variable: lists /. x_?VectorQ :> Sort[x]

If the depth is constant: Map[Sort, lists, {2}]

 
 
 

Sort a List, in a List of Lists of Lists

Post by Patrick Sc » Mon, 15 Nov 2010 20:06:12

Hi,

it's the function Map:

l = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2, 1}, {2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2, 1}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}};
Map[Sort, l, {-2}]

Cheers
Patrick
 
 
 

Sort a List, in a List of Lists of Lists

Post by Albert Ret » Mon, 15 Nov 2010 20:06:23

Am 13.11.2010 06:59, schrieb XXXX@XXXXX.COM :

here are three ways:

lst = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2,
1}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2,
1}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}

goal = {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1,
2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1,
2}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}

In[86]:= res1 = Map[Sort, lst, {2}]

In[88]:= res2 = Map[Sort, lst, {-2}]

In[89]:= res3 = (lst /. (l:{__Integer}) :> Sort[l])

In[90]:= res1 == res2 == res3 == goal

Out[90]= True

hth,

albert
 
 
 

Sort a List, in a List of Lists of Lists

Post by Patrick Sc » Mon, 15 Nov 2010 20:06:33

Hi,

if you take negative numbers for the level spec than it's (in the case
of -2) the second level "from the innerst level in outward direction"

l = {{{{4}}}};
Map[f, l, {2}]
Map[f, l, {-2}]

Cheers
Patrick
 
 
 

Sort a List, in a List of Lists of Lists

Post by Bob Hanlo » Mon, 15 Nov 2010 20:06:55


Map[Sort, data, {2}]


Bob Hanlon



=============
Dear Mathgroup,

I have a lsit of Lists of Lists:

{{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}}

I would like to sort the elements in the lowest level of brackets to give

{{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}}

i.e retaining the same structure with the paired elements in the
original order. I can't seem to get the syntax right to do this apart
from the obvious

{{Sort[{1, 2}], Sort[{2, 1}], Sort[{1, 1}]}, {Sort[{1, 1}],
Sort[{1, 1}], Sort[{1, 2}]}, {Sort[{2, 1}], Sort[{2, 2}],
Sort[{1, 2}]}, {Sort[{2, 2}], Sort[{1, 2}],
Sort[{2, 2}]}, {Sort[{1, 1}], Sort[{2, 1}],
Sort[{1, 2}]}, {Sort[{1, 2}], Sort[{2, 2}], Sort[{2, 2}]}}

I must have a blind spot for the correct command. Can someone please
help me with what should be a straightforward sort. As I want to carry
this out on several hundred thousand pairs I need a more efficient
command. Thanks in advance.

LP

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
 
 
 

Sort a List, in a List of Lists of Lists

Post by leigh.pasc » Mon, 15 Nov 2010 20:07:06

Thanks to everyone who answered.

Of course I tried this first, but neglected to notice the difference
between {2} and (2) (it is very hard to distinguish on my monitor
without my reading glasses!).

LP

Quoting Bob Hanlon < XXXX@XXXXX.COM >:





----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
 
 
 

Sort a List, in a List of Lists of Lists

Post by Helen Rea » Mon, 15 Nov 2010 20:08:12


Look up Map in the Documentation.

list = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2,
1}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2,
1}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}

Map[Sort, list, {2}]


--
Helen Read
 
 
 

Sort a List, in a List of Lists of Lists

Post by Bill Row » Mon, 15 Nov 2010 20:08:44


A simple way to accomplish this is to use Map with a level
specification, that is

In[2]:= Map[Sort, list, {2}]

Out[2]= {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}},
{{1, 2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}},
{{1, 1}, {1, 2}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}
 
 
 

Sort a List, in a List of Lists of Lists

Post by Tomas Garz » Mon, 15 Nov 2010 20:08:55

A quick shot:

In[1]:== a=={{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}};

In[2]:== b=={{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}};

In[3]:== c==Sort/@Sort/@#&/@a

Out[3]== {{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}}

In[4]:== b====c

Out[4]== True

-Tomas
 
 
 

Sort a List, in a List of Lists of Lists

Post by Leonid Shi » Mon, 15 Nov 2010 20:09:29

Hi,

here is your data

data = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2,
1}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2,
1}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}

Mapping Sort on either level 2 or level -2 will do the job, automatically
doing the same
as your hand-written expression with many manually typed Sort commands:

In[3]:= Map[Sort, data, {-2}]

Out[3]= {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1,
2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}}

In[4]:= Map[Sort, data, {2}]

Out[4]= {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1,
2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}}


Regards,
Leonid
 
 
 

Sort a List, in a List of Lists of Lists

Post by Nasser M. » Mon, 15 Nov 2010 20:11:29


one way:

------------------
a={{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}};

{nRow,nCol,nPages}=Dimensions[a];
Map[Sort,Flatten[a,1]];
Partition[%, nCol]
------------------

Out[138]= {{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}}

--Nasser
 
 
 

Sort a List, in a List of Lists of Lists

Post by Nasser M. » Mon, 15 Nov 2010 20:11:40


ps. In the above, {nRow,nCol,nPages}=Dimensions[a] should really be {nPages,nRow,nCol}=Dimensions[a];

If you really want to do this in a more 'obvious' way, you can simply Sort each row in each page
by 'hand' as in

---------------
Table[ Sort[ a[[k,j]] ], {k,1,nPages},{j,1,nRow}]
--------------

Out[407]= {{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}}

There is nothing wrong with this. It says go over each page in
the 3D structure, sort each row. The nice thing is that the
result will have the same shape as the original 3D matrix. So
no need to reshape it using Parition.

I think of a 3D matrix as made up of a book, the number of pages
is the size of the third dimension, and each page in the matrix has
size nRow,nCol. nRow tells how many lines on the page, nCol tells
how many columns in the page.

nPages actually is the first entry returned by Dimensions, not the third.

so, in 3D, when writing A[[ 1,2 ]] the '1' is the page
number, and '2' will be the row number on that page.

in 2D, A[[ 1,2 ]] then '1' is the row number, and '2' is the
column number. There is only one page, so no entry for it.

For 3D, A[[1,2,3]] means page 1, row 2 on that page, column 3 on that page.

For 4D matrix, A[[ n,p,i,j ]] is n'th book, p page in that book, XXXX@XXXXX.COM
i'th row on that page, j'th column

For 5D matrix A[[ m,n,p,i,j ]] is m library, n book in that library,
p page in that book, i row on that page, and j column.

etc...

If you think of the lists as the above, everything becomes very clear
and easy. At leat for me. I do not like to think of lists as nested and
levels and such. I always try to look at a list as arrays and matrices,
and look at what dimensions it has, and go from there.

--Nasser
 
 
 

Sort a List, in a List of Lists of Lists

Post by Oliver Rue » Mon, 15 Nov 2010 20:13:19


Hi,

you could use

data = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2,
1}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2,
1}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}};

Map[f, data, {2}]

{{f[{1, 2}], f[{2, 1}], f[{1, 1}]}, {f[{1, 1}], f[{1, 1}],
f[{1, 2}]}, {f[{2, 1}], f[{2, 2}], f[{1, 2}]}, {f[{2, 2}],
f[{1, 2}], f[{2, 2}]}, {f[{1, 1}], f[{2, 1}],
f[{1, 2}]}, {f[{1, 2}], f[{2, 2}], f[{2, 2}]}}

Map[Sort, data, {2}]

{{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
2}}, {{1, 2}, {2, 2}, {2, 2}}}

HTH,
Oliver
 
 
 

Sort a List, in a List of Lists of Lists

Post by Mauricio E » Mon, 15 Nov 2010 20:52:04

Depending on the different structure of list you might face, this might
prove to be too ad-hoc, but you can have it while waiting for the good
answers

listthing = {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2},
{2,
2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1, 2}}, {{1,
2}, {2, 2}, {2, 2}}};
Dimensions[%]


{6, 3, 2}


Map[Sort[#] &, listthing, Dimensions[listthing][[3]]]


This should work for other types of lists:


Map[Sort[#] &, listthing, Min[Dimensions[listthing]] ]


It is by no means a general solution of course.


Best,


me


2010/11/13 < XXXX@XXXXX.COM >