Re: Re: Re: Sorting a list of pairs on the second elements
- To: mathgroup at smc.vnet.net
- Subject: [mg51441] Re: [mg51412] Re: [mg51372] Re: Sorting a list of pairs on the second elements
- From: DrBob <drbob at bigfoot.com>
- Date: Sun, 17 Oct 2004 03:06:05 -0400 (EDT)
- References: <cklmi7$f1o$1@smc.vnet.net> <200410150647.CAA05265@smc.vnet.net> <200410160820.EAA23720@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
Both methods do the same thing at my machine, and there's also a third method that may do better: sss={{gggcggcgacctcgcgggttttcgctatttatgaaaattttccggtttaaggcgtttccgttcttcttcg \ tcataacttaatgtttttatttaaaatacc,1},{\ tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaaagtatttatcg,13},{\ tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaa,12},{\ tatttttgtaattcaatactgcttcttctt,11},{aaattttccggtttaaggcgtttccgttcttcttcg \ tca,122},{aaattttccggtttaaggcgtttccgttctt,121},{ ttcttgcctttgcgg,1211},{actgcttcttcttgcctttgcggaatttg,1221},{\ gctatttatgaaaattttccggtttaaggcgtttccgttcttcttcgtcataa,133},{\ gctatttatgaaaattttccggtttaaggcg,132},{gctatttat,131},{gcggaatttggcctttta aa, 1321},{aaattttcc,13211},{aatactgcttcttcttgcctttgcggaatttggccttttaaaagt,\ 1331},{tgaaaattttccggttt,13311}}; Sort[sss, (Last[#1] < Last[#2]) &] % == Transpose@Reverse@Transpose@Sort@Transpose@Reverse@Transpose[ sss] %% == Reverse /@ Sort[Reverse /@ sss] {{gggcggcgacctcgcgggttttcgctatttatgaaaattttccggtttaaggcgtttccgttcttcttcg \ tcataacttaatgtttttatttaaaatacc,1},{tatttttgtaattcaatactgcttcttctt,11},{\ tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaa,12},{\ tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaaagtatttatcg,13},{\ aaattttccggtttaaggcgtttccgttctt,121},{aaattttccggtttaaggcgtttccgttcttcttcg \ tca,122},{gctatttat,131},{gctatttatgaaaattttccggtttaaggcg,132},{\ gctatttatgaaaattttccggtttaaggcgtttccgttcttcttcgtcataa,133},{ttcttgcctttgcgg,\ 1211},{actgcttcttcttgcctttgcggaatttg,1221},{aa \ gcggaatttggcctttta,1321},{aatactgcttcttcttgcctttgcggaatttggccttttaaaagt,1331},\ {aaattttcc,13211},{tgaaaattttccggttt,13311}} True True Bobby On Sat, 16 Oct 2004 04:20:51 -0400 (EDT), János <janos.lobb at yale.edu> wrote: > On Oct 15, 2004, at 2:47 AM, Peltio wrote: > >> "János" wrote >> >>> Map[Reverse,Sort[Map[Reverse,collectedDnaBin]]] >> >>> Sort[collectedDnaBin,#1[[2]] < #2[[2]] &] >>> but that does not want to do the trick for me. >> >> I wonder why. It should work, albeit inefficently. Mmmmm, have you >> tried >> with parenthesis wrapped around the pure function? >> Sort[collectedDnaBin,(#1[[2]] < #2[[2]])&] >> Have you tried it in a fresh kernel? > > Yes, and yes. It does not work. > In[31]:= > Sort[sss, (Last[#1] < Last[#2] )&] > > Out[31]= > {{gggcggcgacctcgcgggttttcgctatttatgaaaattttccggtttaaggcgtttccgttcttcttcg > \ > tcataacttaatgtttttatttaaaatacc,1},{\ > tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaaagtatttatcg,13}, > {\ > tatttttgtaattcaatactgcttcttcttgcctttgcggaatttggccttttaaa,12},{\ > tatttttgtaattcaatactgcttcttctt,11},{aaattttccggtttaaggcgtttccgttcttcttcg > tca,\ > 122},{aaattttccggtttaaggcgtttccgttctt,121},{ttcttgcctttgcgg,1211},{\ > actgcttcttcttgcctttgcggaatttg,1221},{\ > gctatttatgaaaattttccggtttaaggcgtttccgttcttcttcgtcataa,133},{\ > gctatttatgaaaattttccggtttaaggcg,132},{gctatttat,131},{gcggaatttggcctttta > aa,\ > 1321},{aaattttcc,13211},{aatactgcttcttcttgcctttgcggaatttggccttttaaaagt,1 > 331},{\ > tgaaaattttccggttt,13311}} > > Maybe because they are Strings ? I am just guessing... >> >>> In general, if I have a list whose elements are m-member lists, like >>> {{a1,...,am},...,{z1,...,zm}}, then my question is what is the >>> best/fastest way to sort it on the k-th elements of the members where >>> 1<=k<=m and ak,...,zk are arbitrary objects - in my case Strings - and >>> use as little memory for it as possible. >> >> Dunno for the general case, but this -very inelegant - approach to sort >> according to the last element seems to be pretty fast (lots of >> procedures >> but applied to the data as a whole, without timeconsuming mapping on >> every >> single element) >> >> Transpose@Reverse@Transpose@Sort@Transpose@Reverse@Transpose[ >> collectedDnaBin ] > > This one works fine. > >> >> ( The first and last three procs just implement a swap function >> swap[list_]:=Transpose@Reverse@Transpose[list] >> ) >> >> The group's gurus will certainly find a proc at least ten times faster >> than >> this, I'm sure. > > I don't know. The suggestions I got via the list and private are all > excellent, but the data I had to sort got so big that I even did not > get to that point to able to sort because the Kernel just quitted on me > half way. Now I am on a different "war-path" to get the results I need > :) /My secret prayers go out for a real 64-bit, or MORE, Mathematica > on OSX..../ > > Thanks a lot, > > János >> >> cheers, >> Peltio >> Invalid address in reply-to. Crafty demunging required to mail me. > > I tried to employ Mathematica in "Crafty demunging" but I failed, so I > send it "as is". Now I embrace for an "Addressee not found..." > thunder... :) >> >> >> > > ------------------------------------------------------------------- > János Löbb > Yale University School of Medicine > Department of Pathology > Phone: 203-737-5204 > Fax: 203-785-7303 > E-mail: janos.lobb at yale.edu > > > > -- DrBob at bigfoot.com www.eclecticdreams.net
- References:
- Re: Sorting a list of pairs on the second elements
- From: "Peltio" <peltio@twilight.zone>
- Re: Re: Sorting a list of pairs on the second elements
- From: János <janos.lobb@yale.edu>
- Re: Sorting a list of pairs on the second elements