f



Sort a List, in a List of Lists of Lists

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.



0
leigh
11/13/2010 5:59:30 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

15 Replies
2070 Views

Similar Articles

[PageSpeed] 31

On Nov 12, 9:59 pm, leigh.pas...@inserm.fr wrote:
> 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.

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}]

0
Raffy
11/14/2010 11:05:50 AM
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

On Sat, 2010-11-13 at 00:59 -0500, leigh.pascoe@inserm.fr wrote:
> 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.
> 
> 
> 


0
Patrick
11/14/2010 11:06:12 AM
Am 13.11.2010 06:59, schrieb leigh.pascoe@inserm.fr:
> 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.

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

0
Albert
11/14/2010 11:06:23 AM
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


On Sat, 2010-11-13 at 14:03 +0100, leigh.pascoe@inserm.fr wrote:
> Ah zut! That's the first thing I tried of course, after scanning the  
> help pages. However I neglected to notice the difference between {2}  
> and (2) (very difficult to distinguish on my monitor). It's curious  
> that the level can be specified by either 2 or -2. Thanks in any case.
> 
> LP
> 
> Quoting Patrick Scheibe <pscheibe@trm.uni-leipzig.de>:
> 
> > 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
> >
> > On Sat, 2010-11-13 at 00:59 -0500, leigh.pascoe@inserm.fr wrote:
> >> 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.
> >>
> >>
> >>
> >
> >
> 
> 
> 
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
> 
> 


0
Patrick
11/14/2010 11:06:33 AM
Map[Sort, data, {2}]


Bob Hanlon

---- leigh.pascoe@inserm.fr wrote: 

=============
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.


0
Bob
11/14/2010 11:06:55 AM
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 <hanlonr@cox.net>:

>
> Map[Sort, data, {2}]
>
>
> Bob Hanlon
>
> ---- leigh.pascoe@inserm.fr wrote:
>
> ==========================
> 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.
>
>



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


0
leigh
11/14/2010 11:07:06 AM
On 11/13/2010 12:59 AM, leigh.pascoe@inserm.fr wrote:
> 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.

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

0
Helen
11/14/2010 11:08:12 AM
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

> Date: Sat, 13 Nov 2010 00:59:27 -0500
> From: leigh.pascoe@inserm.fr
> Subject:  Sort a List, in a List of Lists of Lists
> To: mathgroup@smc.vnet.net
>
> 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.
>
>
>
0
Tomas
11/14/2010 11:08:55 AM
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


On Sat, Nov 13, 2010 at 8:59 AM, <leigh.pascoe@inserm.fr> wrote:

> 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.
>
>
>
>


0
Leonid
11/14/2010 11:09:29 AM
On 11/12/2010 9:59 PM, leigh.pascoe@inserm.fr wrote:
> 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}}}
>

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

0
Nasser
11/14/2010 11:11:29 AM
On 11/13/2010 3:32 PM, Nasser M. Abbasi wrote:

>
> 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}}}
>

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,mathgroup@smc.vnet.net
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

0
Nasser
11/14/2010 11:11:40 AM
On Sat, 13 Nov 2010, leigh.pascoe@inserm.fr wrote:

> 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.
>
>
>
>

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

0
Oliver
11/14/2010 11:13:19 AM
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 <leigh.pascoe@inserm.fr>

> 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
>

0
Mauricio
11/14/2010 11:52:04 AM
leigh.pascoe@inserm.fr wrote:

> {{{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}}}

You might do the following with your list:

Map[Sort,list,{2}]

-- 
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

0
Peter
11/14/2010 11:52:16 AM
Hi,

Setting the Flat attribute for CenterDot seems to cause iteration problems.
By my definitions CenterDot[x] and CenterDot[x,y] should just evaluate
to x and CenterDot[x,y] not the iteration mess below.
Is this a bug?

Thanks,
Dave Snead

In[1]:= $IterationLimit = 20

Out[1]= 20

In[2]:= SetAttributes[CenterDot, Flat]

In[3]:= CenterDot[] := 1

In[4]:= CenterDot[a_] := a

In[5]:= CenterDot[x, y]

During evaluation of In[5]:= $IterationLimit::itlim: Iteration limit of 20 
exceeded. >>

Out[5]= Hold[
1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]x\
\[CenterDot]y]

In[6]:= CenterDot[x]

During evaluation of In[6]:= $IterationLimit::itlim: Iteration limit of 20 
exceeded. >>

Out[6]= Hold[
1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\
\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]1\[CenterDot]x]

In[7]:= CenterDot[]

Out[7]= 1 



0
Dave
11/15/2010 10:52:18 AM
Reply: