Re: [Tutor] sorting data from multiple arrays

2007-03-23 Thread Kent Johnson
Jeff Peery wrote:
> ... what is '*' in '*temp'? thanks!

Say you have a function of 3 arguments:
In [1]: def add(a, b, c):
...: return a+b+c

Normally to call it, you just specify the three arguments:
In [2]: add(1, 2, 3)
Out[2]: 6

Suppose the arguments were already in a list, what would you do? You 
can't just pass the list, that is a single argument and you need three:
In [3]: data=[1, 2, 3]
In [4]: add(data)

Traceback (most recent call last):
   File "", line 1, in 
: add() takes exactly 3 arguments (1 given)

You could unpack the data yourself:
In [5]: a, b, c = data
In [6]: add(a, b, c)
Out[6]: 6

Or you can use the * notation, which basically means, "treat each 
element of this list as a separate argument", or "use this list as the 
argument list directly":
In [7]: add(*data)
Out[7]: 6

If the length of the argument list (data, in the example above) can 
change, manually unpacking the list won't work and the * syntax is the 
only alternative.

Kent

> Decorate-sort-undecorate
> (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234)
> to the rescue:
> 
> In [12]: a = [3,2,1,4]
> In [13]: b = ['hi', 'my','name', 'is']
> In [14]: c = [5,2,4,2]
> In [15]: temp = zip(a, b, c)
> In [16]: temp
> Out[16]: [(3, 'hi', 5), (2, 'my', 2), (1, 'name', 4), (4, 'is', 2)]
> In [17]: temp.sort()
> In [18]: _, b, c = zip(*temp)
> In [19]: b
> Out[19]: ('name', 'my', 'hi', 'is')
> In [20]: c
> Out[20]: (4, 2, 5, 2)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting data from multiple arrays

2007-03-22 Thread Jeff Peery
... what is '*' in '*temp'? thanks!
J

Kent Johnson <[EMAIL PROTECTED]> wrote: Jeff Peery wrote:
> hello, I typically run into this problem and I'm not always sure of the 
> most efficient way to handle it. I often work with multiple arrays of 
> data, say arrays a, b, and c, and I want to sort the elements of b and c 
> based on a. for example:
> 
> a = [3,2,1,4]
> b = ['hi', 'my','name', 'is']
> c = [5,2,4,2]
> 
> I order 'a' from small to large and do the same rearrangement to 'b' and 
> 'c':
> a = [1,2,3,4]
> b = ['name', 'my','hi', 'is']
> c = [4,2,5,2]
> 
> usually I do some terrible looking for loops and iterate over the whole 
> mess is there a clean, efficient way to do this, or is there a nice 
> function that would reorder the elements of b and c based on the soring 
> of a?

Decorate-sort-undecorate 
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234)
to the rescue:

In [12]: a = [3,2,1,4]
In [13]: b = ['hi', 'my','name', 'is']
In [14]: c = [5,2,4,2]
In [15]: temp = zip(a, b, c)
In [16]: temp
Out[16]: [(3, 'hi', 5), (2, 'my', 2), (1, 'name', 4), (4, 'is', 2)]
In [17]: temp.sort()
In [18]: _, b, c = zip(*temp)
In [19]: b
Out[19]: ('name', 'my', 'hi', 'is')
In [20]: c
Out[20]: (4, 2, 5, 2)

Or, if you are a fan of one-liners:
In [21]: _, b, c = zip(*sorted(zip(a, b, c)))

Methinks there should be a clever way to do this with the key= argument 
to sort, but I can't think of it at the moment...

Kent


 
-
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting data from multiple arrays

2007-03-22 Thread Jeff Peery
Thanks for all the responses, that is a huge help!

Jeff

Kent Johnson <[EMAIL PROTECTED]> wrote: Jeff Peery wrote:
> hello, I typically run into this problem and I'm not always sure of the 
> most efficient way to handle it. I often work with multiple arrays of 
> data, say arrays a, b, and c, and I want to sort the elements of b and c 
> based on a. for example:
> 
> a = [3,2,1,4]
> b = ['hi', 'my','name', 'is']
> c = [5,2,4,2]
> 
> I order 'a' from small to large and do the same rearrangement to 'b' and 
> 'c':
> a = [1,2,3,4]
> b = ['name', 'my','hi', 'is']
> c = [4,2,5,2]
> 
> usually I do some terrible looking for loops and iterate over the whole 
> mess is there a clean, efficient way to do this, or is there a nice 
> function that would reorder the elements of b and c based on the soring 
> of a?

Decorate-sort-undecorate 
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234)
to the rescue:

In [12]: a = [3,2,1,4]
In [13]: b = ['hi', 'my','name', 'is']
In [14]: c = [5,2,4,2]
In [15]: temp = zip(a, b, c)
In [16]: temp
Out[16]: [(3, 'hi', 5), (2, 'my', 2), (1, 'name', 4), (4, 'is', 2)]
In [17]: temp.sort()
In [18]: _, b, c = zip(*temp)
In [19]: b
Out[19]: ('name', 'my', 'hi', 'is')
In [20]: c
Out[20]: (4, 2, 5, 2)

Or, if you are a fan of one-liners:
In [21]: _, b, c = zip(*sorted(zip(a, b, c)))

Methinks there should be a clever way to do this with the key= argument 
to sort, but I can't think of it at the moment...

Kent


 
-
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting data from multiple arrays

2007-03-22 Thread Kent Johnson
Jeff Peery wrote:
> hello, I typically run into this problem and I'm not always sure of the 
> most efficient way to handle it. I often work with multiple arrays of 
> data, say arrays a, b, and c, and I want to sort the elements of b and c 
> based on a. for example:
> 
> a = [3,2,1,4]
> b = ['hi', 'my','name', 'is']
> c = [5,2,4,2]
> 
> I order 'a' from small to large and do the same rearrangement to 'b' and 
> 'c':
> a = [1,2,3,4]
> b = ['name', 'my','hi', 'is']
> c = [4,2,5,2]
> 
> usually I do some terrible looking for loops and iterate over the whole 
> mess is there a clean, efficient way to do this, or is there a nice 
> function that would reorder the elements of b and c based on the soring 
> of a?

Decorate-sort-undecorate 
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234)
to the rescue:

In [12]: a = [3,2,1,4]
In [13]: b = ['hi', 'my','name', 'is']
In [14]: c = [5,2,4,2]
In [15]: temp = zip(a, b, c)
In [16]: temp
Out[16]: [(3, 'hi', 5), (2, 'my', 2), (1, 'name', 4), (4, 'is', 2)]
In [17]: temp.sort()
In [18]: _, b, c = zip(*temp)
In [19]: b
Out[19]: ('name', 'my', 'hi', 'is')
In [20]: c
Out[20]: (4, 2, 5, 2)

Or, if you are a fan of one-liners:
In [21]: _, b, c = zip(*sorted(zip(a, b, c)))

Methinks there should be a clever way to do this with the key= argument 
to sort, but I can't think of it at the moment...

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting data from multiple arrays

2007-03-22 Thread R. Alan Monroe
> hello, I typically run into this problem and I'm not always sure of the most 
> efficient way to handle it. I often work with multiple arrays of data, say 
> arrays a, b, and c, and I want to sort the
> elements of b and c based on a. for example:

> a = [3,2,1,4]
> b = ['hi', 'my','name', 'is']
> c = [5,2,4,2]

> I order 'a' from small to large and do the same rearrangement to 'b' and 'c':
> a = [1,2,3,4]
>  b = ['name', 'my','hi', 'is']
>  c = [4,2,5,2]

> usually I do some terrible looking for loops and iterate over the whole 
> mess is there a clean, efficient way to do this, or is there a nice 
> function that would reorder the elements of b and c
> based on the soring of a?

Is switching to a dictionary an option?

mystuff = {3: ('hi', 5),
   2: ('my', 2),
   1: ('name', 4),
   4: ('is', 2)}

You can get a list of the keys, sort _that_, then fetch from the dict
based on the sorted list.

Alan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor