Re: sort one list using the values from another list

2006-02-28 Thread bearophileHUGS
_ is just a plain variable name in Python. It is sometimes when a variable is needed to receive a value that won't be used. Like in some other interactive systems (Mathematica, etc, but with a different syntax) _ has a use in the interactive shell, it contains the last unassigned result: a = 2

Re: sort one list using the values from another list

2006-02-28 Thread Ron Adam
[EMAIL PROTECTED] wrote: Following Ron Adam solution (and using [] instead of list() in the last line), this may be a possible solution of the problem, that is often quite fast: def psort16(s1, s2): try: d = dict(izip(s2, s1)) except TypeError: _indices =

Re: sort one list using the values from another list

2006-02-27 Thread Ron Adam
Ron Adam wrote: Alex Martelli wrote: Ron Adam [EMAIL PROTECTED] wrote: ... Considering the number time I sort keys after getting them, It's the behavior I would prefer. Maybe a more dependable dict.sortedkeys() method would be nice. ;-) sorted(d) is guaranteed to do exactly the same

Re: sort one list using the values from another list

2006-02-27 Thread Dolmans Sun
Kent Johnson wrote: [a for b,a in sorted(zip(B,A))] also, [a for _,a in sorted(zip(B,A))] didn't read refs, tested above python-2.2.3. -- Sun Yi Ming you can logout any time you like, but you can never leave... -- http://mail.python.org/mailman/listinfo/python-list

Re: sort one list using the values from another list

2006-02-27 Thread Magnus Lycka
Dolmans Sun wrote: Kent Johnson wrote: [a for b,a in sorted(zip(B,A))] also, [a for _,a in sorted(zip(B,A))] didn't read refs, tested above python-2.2.3. Is there something here I can't see, or did you just change a variable name and present that as another solution? --

Re: sort one list using the values from another list

2006-02-27 Thread Scott David Daniels
Ron Adam wrote: Ron Adam wrote: Alex Martelli wrote: Ron Adam [EMAIL PROTECTED] wrote: ... Considering the number time I sort keys after getting them, It's the behavior I would prefer. Maybe a more dependable dict.sortedkeys() method would be nice. ;-) sorted(d) is guaranteed to do

Re: sort one list using the values from another list

2006-02-27 Thread Ron Adam
Scott David Daniels wrote: Ron Adam wrote: Ron Adam wrote: This probably should be: def psort11(s1, s2): d = dict(zip(s2,s1)) assert len(d) == len(s1) s1[:] = list(d[v] for v in sorted(d)) You could do this to avoid all of those lookups: def psort_xx(s1, s2):

Re: sort one list using the values from another list

2006-02-27 Thread bearophileHUGS
Following Ron Adam solution (and using [] instead of list() in the last line), this may be a possible solution of the problem, that is often quite fast: def psort16(s1, s2): try: d = dict(izip(s2, s1)) except TypeError: _indices = range(len(s1))

Re: sort one list using the values from another list

2006-02-27 Thread Simon Sun
Magnus Lycka wrote: Is there something here I can't see, or did you just change a variable name and present that as another solution? Heh, I have use python for a very short time. Base your statements, I test in python, found `_' is a valid variable name. So I don't know it is a just a plain

Re: sort one list using the values from another list

2006-02-27 Thread Kent Johnson
Simon Sun wrote: Magnus Lycka wrote: Is there something here I can't see, or did you just change a variable name and present that as another solution? Heh, I have use python for a very short time. Base your statements, I test in python, found `_' is a valid variable name. So I don't know it

sort one list using the values from another list

2006-02-26 Thread Brian Blais
Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A=['hello','there','this','that'] B=[3,4,2,5] I'd like to sort list A using the values from B, so the result would be in this example, A=['this','hello','there','that'] The sort method on

Re: sort one list using the values from another list

2006-02-26 Thread Kent Johnson
Brian Blais wrote: Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A=['hello','there','this','that'] B=[3,4,2,5] I'd like to sort list A using the values from B, so the result would be in this example,

Re: sort one list using the values from another list

2006-02-26 Thread Jeffrey Schwab
Brian Blais wrote: Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A=['hello','there','this','that'] B=[3,4,2,5] I'd like to sort list A using the values from B, so the result would be in this example,

Re: sort one list using the values from another list

2006-02-26 Thread Alex Martelli
Brian Blais [EMAIL PROTECTED] wrote: Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A=['hello','there','this','that'] B=[3,4,2,5] I'd like to sort list A using the values from B, so the result would be in this example,

Re: sort one list using the values from another list

2006-02-26 Thread Steven Bethard
Brian Blais wrote: Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A=['hello','there','this','that'] B=[3,4,2,5] I'd like to sort list A using the values from B, so the result would be in this example,

Re: sort one list using the values from another list

2006-02-26 Thread Duncan Booth
Steven Bethard wrote: Here's a solution that makes use of the key= argument to sorted(): A = ['hello','there','this','that'] B = [3,4,2,5] indices = range(len(A)) indices.sort(key=B.__getitem__) [A[i] for i in indices] ['this', 'hello', 'there', 'that'] Basically, it sorts the

Re: sort one list using the values from another list

2006-02-26 Thread bearophileHUGS
Your solution Steven Bethard looks very intelligent, here is a small speed test, because sorting a list according another one is a quite common operation. (Not all solutions are really the same, as Alex has shown). from itertools import izip, imap from operator import itemgetter from random

Re: sort one list using the values from another list

2006-02-26 Thread Ron Adam
[EMAIL PROTECTED] wrote: Your solution Steven Bethard looks very intelligent, here is a small speed test, because sorting a list according another one is a quite common operation. (Not all solutions are really the same, as Alex has shown). Try this one. def psort10(s1, s2): d =

Re: sort one list using the values from another list

2006-02-26 Thread Alex Martelli
Ron Adam [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: Your solution Steven Bethard looks very intelligent, here is a small speed test, because sorting a list according another one is a quite common operation. (Not all solutions are really the same, as Alex has shown). Try this

Re: sort one list using the values from another list

2006-02-26 Thread bearophileHUGS
It's faster on my system because d.keys() is already sorted. But that may not be the case on other versions of python. In my version it's a little slower. But what system are you using where keys is already sorted? IronPython maybe? Bye, bearophile --

Re: sort one list using the values from another list

2006-02-26 Thread Ron Adam
Alex Martelli wrote: Ron Adam [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: Your solution Steven Bethard looks very intelligent, here is a small speed test, because sorting a list according another one is a quite common operation. (Not all solutions are really the same, as Alex has

Re: sort one list using the values from another list

2006-02-26 Thread Ron Adam
[EMAIL PROTECTED] wrote: It's faster on my system because d.keys() is already sorted. But that may not be the case on other versions of python. In my version it's a little slower. But what system are you using where keys is already sorted? IronPython maybe? Bye, bearophile Python

RE: sort one list using the values from another list

2006-02-26 Thread Delaney, Timothy (Tim)
Ron Adam wrote: Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 I was a bit surprised by them being sorted. I just happend to try d.keys() in place of s2, and it sped up. I was expecting it to be a bit slower. Purely an implementation accident - based

Re: sort one list using the values from another list

2006-02-26 Thread Alex Martelli
Ron Adam [EMAIL PROTECTED] wrote: ... Considering the number time I sort keys after getting them, It's the behavior I would prefer. Maybe a more dependable dict.sortedkeys() method would be nice. ;-) sorted(d) is guaranteed to do exactly the same thing as sorted(d.keys()) AND to be

Re: sort one list using the values from another list

2006-02-26 Thread Ron Adam
Alex Martelli wrote: Ron Adam [EMAIL PROTECTED] wrote: ... Considering the number time I sort keys after getting them, It's the behavior I would prefer. Maybe a more dependable dict.sortedkeys() method would be nice. ;-) sorted(d) is guaranteed to do exactly the same thing as

Re: sort one list using the values from another list

2006-02-26 Thread Ron Adam
Delaney, Timothy (Tim) wrote: Ron Adam wrote: Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 I was a bit surprised by them being sorted. I just happend to try d.keys() in place of s2, and it sped up. I was expecting it to be a bit slower. Purely an