Re: [Tutor] Sorting 2-d data

2009-09-13 Thread Patrick Sabin
> But for sorting the list with the first element as key, I tried it using just mylist.sort() without the lambda, and its working also. Then why use the lambda? There is a little difference between those two variations. Example: >>> sorted([[1,2],[1,3],[1,1]]) [[1, 1], [1, 2], [1, 3]] >>> s

Re: [Tutor] Sorting 2-d data

2009-09-13 Thread Dirk Wangsadirdja
Lie Ryan wrote: if you have data like this: mylist = [ [3, 4, 3, 2, 1], [2, 1, 1, 1, 1], [4, 2, 2, 1, 2], [1, 3, 1, 1, 1], ] you can use mylist.sort(key=lambda x: x[0]) sorted(mylist, key=lambda x: x[0]) works as well if you need a new list I have a further question. If I wa

Re: [Tutor] Sorting 2-d data

2009-09-13 Thread Kent Johnson
On Sun, Sep 13, 2009 at 1:17 AM, Lie Ryan wrote: > if you have data like this: > mylist = [ >    [3, 4, 3, 2, 1], >    [2, 1, 1, 1, 1], >    [4, 2, 2, 1, 2], >    [1, 3, 1, 1, 1], > ] > > you can use mylist.sort(key=lambda x: x[0]) You can omit the key parameter completely in this case, unless yo

Re: [Tutor] Sorting 2-d data

2009-09-13 Thread Wayne
On Sun, Sep 13, 2009 at 7:27 AM, Rich Lovely wrote: > 2009/9/13 Lie Ryan : > > Wayne wrote: > >> > > if your data is like this: > > mylist = [ > >[3, 2, 4, 1], > >[4, 1, 2, 3], > >[3, 1, 2, 1], > >[2, 1, 1, 1], > >[1, 1, 2, 1], > > ] > > > > you can use zip(*mylist) to transfo

Re: [Tutor] Sorting 2-d data

2009-09-13 Thread Rich Lovely
2009/9/13 Lie Ryan : > Wayne wrote: >> >> Hi, >> >> I have a set of data that looks something like this: >> >> 3, 4, 3, 2, 1 >> 2, 1, 1, 1, 1 >> 4, 2, 2, 1, 2 >> 1, 3, 1, 1, 1 >> >> I want to be able to sort it by the first column, keeping the rest of the >> values in the same position relative to

Re: [Tutor] Sorting 2-d data

2009-09-12 Thread Lie Ryan
Wayne wrote: Hi, I have a set of data that looks something like this: 3, 4, 3, 2, 1 2, 1, 1, 1, 1 4, 2, 2, 1, 2 1, 3, 1, 1, 1 I want to be able to sort it by the first column, keeping the rest of the values in the same position relative to the original: 1, 3, 1, 1, 1 2, 1, 1, 1, 1 3, 4, 3,

[Tutor] Sorting 2-d data

2009-09-12 Thread Wayne
Hi, I have a set of data that looks something like this: 3, 4, 3, 2, 1 2, 1, 1, 1, 1 4, 2, 2, 1, 2 1, 3, 1, 1, 1 I want to be able to sort it by the first column, keeping the rest of the values in the same position relative to the original: 1, 3, 1, 1, 1 2, 1, 1, 1, 1 3, 4, 3, 2, 1 4, 2, 2, 1, 2