sorting two corresponding lists?

Arnaud Delobelle arnodel at googlemail.com
Fri Apr 24 03:18:21 EDT 2009


On Apr 24, 2:32 am, "Hans DushanthaKumar"
<hans.dushanthaku... at hcn.com.au> wrote:
> 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)]

It's also the same as

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

Which reduces to

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

(Although 'i' is not a good choice of variable as it represents a
value, not an index)

Anyway it doesn't work well if the 'values' list has repeated values,
e.g.

items = ['spam', 'eggs', 'wafer']
values = [3, 7, 3]

--
Arnaud



More information about the Python-list mailing list