Duncan Booth wrote:
Try this:

l = [1, 2, 3, 4]
for a, b in zip(*[iter(l)]*2):
   print a, b

zip(*[iter(seq)]*N) will group by N (but if there are any odd items at the end it will ignore them).

map(None, *[iter(seq)]*N) will group by N padding the last item with None if it needs to.

For anyone else who was as bemused as I was that Duncan's and F. Petitjean's suggestions actually *work*, this was what I had to do to figure out *why* they work:


Py> l = [1, 2, 3, 4]
Py> itr = iter(l)
Py> zip(itr) # Put all items from iterator in position 1
[(1,), (2,), (3,), (4,)]
Py> itr = iter(l)
Py> zip(itr, itr) # Put every second item in position 2
[(1, 2), (3, 4)]

Using zip(*[iter(l)]*N) or zip(*(iter(l),)*N) simply extends the above to the general case.

I'd definitely recommend hiding this trick inside a function. Perhaps something like (using Michael's function name):

from itertools import izip, repeat, chain

def partition(seq, part_len):
  return izip(*((iter(seq),) * part_len))

def padded_partition(seq, part_len, pad_val=None):
  itr = iter(seq)
  if (len(seq) % part_len != 0):
    padding = repeat(pad_val, part_len)
    itr = chain(itr, padding)
  return izip(*((itr,) * part_len))

Py> list(partition(range(10), 2))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
Py> list(partition(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
Py> list(padded_partition(range(10), 2))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
Py> list(padded_partition(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
Py> list(padded_partition(range(10), 3, False))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, False, False)]
Py> zip(*padded_partition(range(10), 3))
[(0, 3, 6, 9), (1, 4, 7, None), (2, 5, 8, None)]

Not sure how useful that last example is, but I thought it was cute :)

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to