sort the list

2005-11-21 Thread Shi Mu
I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list based on the second value in the item? That is, I want the list to be: [[3,2],[1,4],[2,5],[3,9]] -- http://mail.python.org/mailman/listinfo/python-list

Re: sort the list

2005-11-21 Thread Daniel Schüle
Shi Mu wrote: > I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list > based on the second value in the item? > That is, > I want the list to be: > [[3,2],[1,4],[2,5],[3,9]] >>> lst = [[1,4],[3,9],[2,5],[3,2]] >>> lst [[1, 4], [3, 9], [2, 5]

Re: sort the list

2005-11-21 Thread Shi Mu
On 11/21/05, Daniel Schüle <[EMAIL PROTECTED]> wrote: > Shi Mu wrote: > > I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list > > based on the second value in the item? > > That is, > > I want the list to be: > > [[3,2],[1,4],[2,5],[3,9]] &

Re: sort the list

2005-11-21 Thread Fredrik Lundh
Shi Mu wrote: > I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list > based on the second value in the item? > That is, > I want the list to be: > [[3,2],[1,4],[2,5],[3,9]] since you seem to be using 2.3, the solution is to use a custom compare function:

Re: sort the list

2005-11-21 Thread Daniel Schüle
[...] >> >>> lst = [[1,4],[3,9],[2,5],[3,2]] >> >>> lst >>[[1, 4], [3, 9], [2, 5], [3, 2]] >> >>> >> >>> >> >>> lst.sort(cmp = lambda x,y: cmp(x[1], y[1])) >> >>> lst >>[[3, 2], [1, 4], [2, 5], [3, 9]] >> >>> >> >>works for Python 2.4 >>in earlier Pythons just let cmp = .. away >> >>Regards, Danie

Re: sort the list

2005-11-21 Thread Fredrik Lundh
Daniel Schüle wrote: > > what does let cmp = .. away mean? > > it means > lst.sort(lambda x,y: cmp(x[1], y[1])) note that cmp= isn't needed under 2.4 either. if you leave it out, your code will be more portable. -- http://mail.python.org/mailman/listinfo/python-list

Re: sort the list

2005-11-21 Thread Giovanni Bajo
Fredrik Lundh wrote: >> I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list >> based on the second value in the item? >> That is, >> I want the list to be: >> [[3,2],[1,4],[2,5],[3,9]] > > since you seem to be using 2.3, the solution i

Re: sort the list

2005-11-21 Thread Duncan Booth
Daniel Schüle wrote: > I can offer you some more brain food to digest ;) > maybe you can adapt this solution, but that depends > on your problem > I find it clear and I used it recently > > >>> name, age, salary = "name", "age", "salary" > >>> people = [ > ... {name:"oliver", age:25, salary:1800}

Re: sort the list

2005-11-22 Thread Nick Craig-Wood
Daniel Schüle <[EMAIL PROTECTED]> wrote: > lst.sort(lambda x,y: cmp(x[1], y[1])) Since no-one mentioned it and its a favourite of mine, you can use the decorate-sort-undecorate method, or "Schwartzian Transform" eg lst = [[1,4],[3,9],[2,5],[3,2]] # decorate - ie make a copy of each item with th

Re: sort the list

2005-11-22 Thread Neil Hodgson
Nick Craig-Wood: > Since no-one mentioned it and its a favourite of mine, you can use the > decorate-sort-undecorate method, or "Schwartzian Transform" That is what the aforementioned key argument to sort is: a built-in decorate-sort-undecorate. >>> lst = [[1,4],[3,9],[2,5],[3,2]] >>> pri

Re: sort the list

2005-11-23 Thread Duncan Booth
Neil Hodgson wrote: >> Since no-one mentioned it and its a favourite of mine, you can use the >> decorate-sort-undecorate method, or "Schwartzian Transform" > > That is what the aforementioned key argument to sort is: a built-in > decorate-sort-undecorate. And crucially it is a built-in DSU

Re: sort the list

2005-11-23 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Duncan Booth wrote: >> e.g. it is stable when you reverse the order: >> >> >>> lst = [[4,1],[4,2],[9,3],[5,4],[2,5]] >> >>> list(reversed([ x[-1] for x in sorted([ (x[0],x) for x in lst ]) ])) >> [[9, 3], [5, 4], [4, 2], [4, 1], [2, 5]] >> >>> l1 = list(lst) >> >>> l1.so

Re: sort the list

2005-11-23 Thread [EMAIL PROTECTED]
Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > > Duncan Booth wrote: > >> e.g. it is stable when you reverse the order: > >> > >> >>> lst = [[4,1],[4,2],[9,3],[5,4],[2,5]] > >> >>> list(reversed([ x[-1] for x in sorted([ (x[0],x) for x in lst ]) ])) > >> [[9, 3], [5, 4], [4, 2], [4, 1], [2, 5]]

Re: sort the list

2005-11-23 Thread [EMAIL PROTECTED]
Duncan Booth wrote: > e.g. it is stable when you reverse the order: > > >>> lst = [[4,1],[4,2],[9,3],[5,4],[2,5]] > >>> list(reversed([ x[-1] for x in sorted([ (x[0],x) for x in lst ]) ])) > [[9, 3], [5, 4], [4, 2], [4, 1], [2, 5]] > >>> l1 = list(lst) > >>> l1.sort(key=operator.itemgetter(0), rev

Re: sort the list

2005-11-23 Thread Nick Craig-Wood
Neil Hodgson <[EMAIL PROTECTED]> wrote: > Nick Craig-Wood: > > > Since no-one mentioned it and its a favourite of mine, you can use the > > decorate-sort-undecorate method, or "Schwartzian Transform" > > That is what the aforementioned key argument to sort is: a built-in > decorate-sort-u