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
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
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]]
[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
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
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
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
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}
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
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
[...]
>> >>> 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
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:
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]]
&
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]
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
15 matches
Mail list logo