"multi" zip

Gerhard Häring gerhard.haering at gmx.de
Thu Jun 27 12:23:51 EDT 2002


Am Thu, 27 Jun 2002 15:46:25 GMT schrieben Sie (Mark <Aristotle_00 at yahoo.com>):
> So,
> 
> Let line be a list of lists where the sublists are the same length.
> 
> For example:
> 
> line = [ [0,1,2], [3,4,5], [6,7,8] ]
> 
> Now, I can do:
> 
> zip(line[0], line[1], line[2])
> 
> to "invert" the array.  Now, I'd like to do two different things:
> 
> 1) instead of producing tuples, I'd like to produce lists.
> 
> 2) I'd like to extend this to an arbitrary number of lists.  If it were
> possible, I'd do zip(line[0], line[1], ..., line[n]) but this doesn't seem to
> compile :).
> 
> Is there a way to make zip() do this, or should I stop trying to hammer with
> a wrench?

zip(*list)

The *sequence form of calls is described in chapter 5.3.4 of the language
reference: http://www.python.org/doc/current/ref/calls.html

Gerhard
--
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))






More information about the Python-list mailing list