Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'need something similar to gatherby' topicStudent Support Forum > General > "need something similar to gatherby"

Next Comment >Help | Reply To Topic
Author Comment/Response
prettyperla
09/15/11 5:02pm

see attached document.
I have this list {{1,1,"a"},{2,2,"b"},{3,3,"b"},{4,4,"c"},{5,5,"c"},{6,6,"a"},{7,7,"a"},{8,8,"a"},{9,9,"c"},{10,10,"c"},{11,11,"d"},{12,12,"d"}} and I want to seperate based on the third position. I used GatherBy and it has completed the task, the problem is that I need to keep the order the same, so I need a function that will have this output instead of what I have at the attempt at the bottom. I also need to incoporate all of the list without cutting anything out. thanks much!


perlalist={{1,1,"a"},{2,2,"b"},{3,3,"b"},{4,4,"c"},{5,5,"c"},{6,6,"a"},{7,7,"a"},{8,8,"a"},{9,9,"c"},{10,10,"c"},{11,11,"d"},{12,12,"d"}}

successgoalcomplete={{{1,1,"a"}},{{2,2,"b"},{3,3,"b"}},{{4,4,"c"},{5,5,"c"}},{{6,6,"a"},{7,7,"a"},{8,8,"a"}},{{9,9,"c"},{10,10,"c"}},{{11,11,"d"},{12,12,"d"}}}



below is the attempt that does not work

In[410]:= perlalist = {{1, 1, "a"}, {2, 2, "b"}, {3, 3, "b"}, {4, 4, "c"}, {5, 5,
"c"}, {6, 6, "a"}, {7, 7, "a"}, {8, 8, "a"}, {9, 9, "c"}, {10, 10,
"c"}, {11, 11, "d"}, {12, 12, "d"}};

failedattempt = GatherBy[perlalist, Last]

Out[411]= {{{1, 1, "a"}, {6, 6, "a"}, {7, 7, "a"}, {8, 8, "a"}}, {{2, 2, "b"}, {3, 3,
"b"}}, {{4, 4, "c"}, {5, 5, "c"}, {9, 9, "c"}, {10, 10, "c"}}, {{11, 11,
"d"}, {12, 12, "d"}}}

Attachment: experiments.nb, URL: ,

Subject (listing for 'need something similar to gatherby')
Author Date Posted
need something similar to gatherby prettyperla 09/15/11 5:02pm
Re: need something similar to gatherby yehuda ben-s... 09/16/11 01:44am
Next Comment >Help | Reply To Topic