list to list of pairs
Werner Schiendl
ws-news at gmx.at
Tue Nov 20 05:01:37 EST 2001
Hi,
here an alternative way using generators (new in Python 2.2)
from __future__ import generators
def pieces(seq, num_item=2):
start = 0
while 1:
item = seq[start:start+num_item]
if not item:
break
yield item
start += num_item
result = [item for item in pieces([1, 2, 3, 4, 5, 6])]
print result
result = [item for item in pieces([1, 2, 3, 4, 5, 6], 3)]
print result
- produces
[[1, 2], [3, 4], [5, 6]]
[[1, 2, 3], [4, 5, 6]]
hth
Werner
<bvdpoel at uniserve.com> wrote in message
news:3BF94B42.346E96EF at uniserve.com...
>
> I've sure I've seen this somewhere...but I can't today...
>
> I need to convert a list like [1,2,3,4,5,6] to a list of pairs [[1,2],
> [3,4], [5,6]]. Isn't there a way to do this with map() or zip()???
>
> --
> Bob van der Poel ** Wynndel, British Columbia, CANADA **
> EMAIL: bvdpoel at uniserve.com
> WWW: http://users.uniserve.com/~bvdpoel
More information about the Python-list
mailing list