Re: sorting two corresponding lists?

2009-04-24 Thread Arnaud Delobelle
On Apr 24, 2:32 am, Hans DushanthaKumar hans.dushanthaku...@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

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-23 Thread Piet van Oostrum
Saketh saketh.bhamidip...@gmail.com (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

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(new_values)

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:) items = ['apple', 'car', 'town

Re: sorting two corresponding lists?

2009-04-21 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] I would like to sort the 'items

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

Re: sorting two corresponding lists?

2009-04-21 Thread 邓弈龙
://0047/!x-usc:mailto:ebo...@hotmail.com To: python-list@python.orgmhtml:{154E1F55-D942-4CE9-9B8C-82060E1E80CE}mid://0047/!x-usc:mailto:python-list@python.org Sent: Tuesday, April 21, 2009 12:10 AM Subject: sorting two corresponding lists? Hello all, I wonder if someone could help me

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 the 'zip' version is

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)

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' list based

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 = [5, 2, 7, 1] I would like to sort

Re: sorting two corresponding lists?

2009-04-20 Thread Saketh
On Apr 20, 12:10 pm, Esmail ebo...@hotmail.com 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

Re: sorting two corresponding lists?

2009-04-20 Thread Luis Alberto Zarrabeitia Gomez
Quoting Esmail ebo...@hotmail.com: 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:

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 =

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

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 Arnaud Delobelle
Esmail ebo...@hotmail.com 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 = [5, 2, 7, 1] I