sorting two corresponding lists?

Hans DushanthaKumar hans.dushanthakumar at hcn.com.au
Fri Apr 24 03:32:41 CEST 2009


Just being pedantic here :)

[items[x] for x in [i for i in map(values.index, new_values)]]

Is the same as

[items[x] for x in map(values.index, new_values)]



-----Original Message-----
From: python-list-bounces+hans.dushanthakumar=hcn.com.au at python.org
[mailto:python-list-bounces+hans.dushanthakumar=hcn.com.au at python.org]
On Behalf Of Esmail
Sent: Friday, 24 April 2009 3:02 AM
To: tiefeng wu
Cc: python-list at python.org
Subject: Re: sorting two corresponding lists?


> My solution, I know the 'zip' version is more elegant, this is just
for 
> fun:)
>  
>  >>> items = ['apple', 'car', 'town', 'phone']
>  >>> values = [5, 2, 7, 1]
>  >>> new_values = sorted(values, reverse = True)
>  >>> new_items = [items[x] for x in [i for i in map(values.index, 
> new_values)]]
>  >>> print(new_values)
> [7, 5, 2, 1]
>  >>> print(new_items)
> ['town', 'apple', 'car', 'phone']
>  >>>
>  

Cool .. always good to know alternative ways of accomplishing
a task.

Esmail
--
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list