Re: sorting two corresponding lists?

2009-04-24 Thread tiefeng wu
> > > 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 >

Re: sorting two corresponding lists?

2009-04-24 Thread Arnaud Delobelle
On Apr 24, 2:32 am, "Hans DushanthaKumar" 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_va

RE: sorting two corresponding lists?

2009-04-23 Thread Hans DushanthaKumar
+hans.dushanthakumar=hcn.com...@python.org] On Behalf Of Esmail Sent: Friday, 24 April 2009 3:02 AM To: tiefeng wu Cc: python-list@python.org Subject: Re: sorting two corresponding lists? > My solution, I know the 'zip' version is more elegant, this is just for > fun:) > > >>>

Re: sorting two corresponding lists?

2009-04-23 Thread Esmail
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

Re: sorting two corresponding lists?

2009-04-23 Thread Piet van Oostrum
> Saketh (S) wrote: >S> Why not use a dictionary instead of two lists? Then you can sort the >S> dictionary by value -- e.g. >S> d = dict(zip(items, values)) >S> sorted_items = sorted(d.iteritems(), key=lambda (k,v): (v,k)) >S> This produces a list of pairs, but demonstrates the general ide

Re: sorting two corresponding lists?

2009-04-21 Thread nathanielpeterson08
>From http://wiki.python.org/moin/HowTo/Sorting#Sortingbykeys, using the >Decorate-Sort-Undecorate (aka Schwartzian transform) idiom: #!/usr/bin/env python items = ['apple', 'car', 'town', 'phone'] values = [5, 2, 7, 1] zipped=zip(values,items) zipped.sort(reverse=True) values_sorted,items_sorte

Re: sorting two corresponding lists?

2009-04-21 Thread tiefeng wu
> > Esmail wrote: > > > items = [apple, car, town, phone] > > values = [5, 2, 7, 1] > > > > I would like to sort the 'items' list based on the 'values' list so > > that I end up with the following two list: > > > > items = [town, apple, car, phone] > > values = [7, 5, 2, 1] My solution, I know th

Re: sorting two corresponding lists?

2009-04-21 Thread 邓弈龙
0 AM Subject: sorting two corresponding lists? > Hello all, > > I wonder if someone could help me with sorting two corresponding lists. > > For instance the first list contains some items, and the second list > contains their value (higher is better) > > items = [apple, car,

Re: sorting two corresponding lists?

2009-04-21 Thread Esmail
Esmail wrote: items = [apple, car, town, phone] values = [5, 2, 7, 1] I would like to sort the 'items' list based on the 'values' list so that I end up with the following two list: items = [town, apple, car, phone] values = [7, 5, 2, 1] Hello all, thanks for all the great suggestions. I use

Re: sorting two corresponding lists?

2009-04-20 Thread Peter Otten
Esmail wrote: > I wonder if someone could help me with sorting two corresponding lists. > > For instance the first list contains some items, and the second list > contains their value (higher is better) > > items = [apple, car, town, phone] > values = [5, 2, 7, 1] > &g

Re: sorting two corresponding lists?

2009-04-20 Thread Arnaud Delobelle
Esmail writes: > Hello all, > > I wonder if someone could help me with sorting two corresponding lists. > > For instance the first list contains some items, and the second list > contains their value (higher is better) > > items = [apple, car, town, phone] > values

Re: sorting two corresponding lists?

2009-04-20 Thread Esmail
Thanks Luis, more code for me to study and learn from. Esmail Luis Alberto Zarrabeitia Gomez wrote: I've used this sometimes: -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting two corresponding lists?

2009-04-20 Thread Esmail
Saketh wrote: Why not use a dictionary instead of two lists? Then you can sort the dictionary by value -- e.g. thanks for the suggestion. I am not sure this is quite suitable for my application (the example I provided was extremely simplified), but this is a useful technique to know and has b

Re: sorting two corresponding lists?

2009-04-20 Thread Esmail
Hi Diez, Thanks for this, I had seen zip() before but had no idea really what it does, this will serve as good motivation to find out more. I'm amazed at what this language can do (and the helpfulness of the people on the list here). Best, Esmail Diez B. Roggisch wrote: items = zip(*sorted(z

Re: sorting two corresponding lists?

2009-04-20 Thread Luis Alberto Zarrabeitia Gomez
Quoting Esmail : > items = [apple, car, town, phone] > values = [5, 2, 7, 1] > > I would like to sort the 'items' list based on the 'values' list so > that I end up with the following two list: > > items = [town, apple, car, phone] > values = [7, 5, 2, 1] I've used this sometimes: === [unte

Re: sorting two corresponding lists?

2009-04-20 Thread Saketh
On Apr 20, 12:10 pm, Esmail wrote: > Hello all, > > I wonder if someone could help me with sorting two corresponding lists. > > For instance the first list contains some items, and the second list > contains their value (higher is better) > > items = [apple, car, town, ph

Re: sorting two corresponding lists?

2009-04-20 Thread Diez B. Roggisch
Esmail wrote: > Hello all, > > I wonder if someone could help me with sorting two corresponding lists. > > For instance the first list contains some items, and the second list > contains their value (higher is better) > > items = [apple, car, town, phone] > values

sorting two corresponding lists?

2009-04-20 Thread Esmail
Hello all, I wonder if someone could help me with sorting two corresponding lists. For instance the first list contains some items, and the second list contains their value (higher is better) items = [apple, car, town, phone] values = [5, 2, 7, 1] I would like to sort the 'items'