Re: Iterate through list two items at a time

2007-01-04 Thread Wade Leftwich
Peter Otten wrote: > Wade Leftwich wrote: > > > from itertools import groupby > > > > def chunk(it, n=0): > > if n == 0: > > return iter([it]) > > def groupfun((x,y)): > > return int(x/n) > > grouped = groupby(enumerate(it), groupfun) > > counted = (y for (x,y) in gr

Re: Iterate through list two items at a time

2007-01-04 Thread Peter Otten
Wade Leftwich wrote: > from itertools import groupby > > def chunk(it, n=0): > if n == 0: > return iter([it]) > def groupfun((x,y)): > return int(x/n) > grouped = groupby(enumerate(it), groupfun) > counted = (y for (x,y) in grouped) > return ((z for (y,z) in x)

Re: Iterate through list two items at a time

2007-01-04 Thread Wade Leftwich
Wade Leftwich wrote: > Jeffrey Froman wrote: > > Dave Dean wrote: > > > > > I'm looking for a way to iterate through a list, two (or more) items at a > > > time. > > > > Here's a solution, from the iterools documentation. It may not be the /most/ > > beautiful, but it is short, and scales well fo

Re: Iterate through list two items at a time

2007-01-03 Thread Wade Leftwich
Jeffrey Froman wrote: > Dave Dean wrote: > > > I'm looking for a way to iterate through a list, two (or more) items at a > > time. > > Here's a solution, from the iterools documentation. It may not be the /most/ > beautiful, but it is short, and scales well for larger groupings: > > >>> from iter

Re: Iterate through list two items at a time

2007-01-03 Thread J. Clifford Dyer
Gabriel Genellina wrote: > b=iter(a) > for x in b: >y=b.next() >print x,y > So as not to choke on odd-length lists, you could try a = [1,2,3,4,5,6,7] b = iter(a) for x in b: try: y=b.next() except StopIteration: y=None print x,y Substitute in whatever for y

Re: Iterate through list two items at a time

2007-01-02 Thread Gerard Flanagan
Dave Dean wrote: > Hi all, > I'm looking for a way to iterate through a list, two (or more) items at a > time. Basically... > > myList = [1,2,3,4,5,6] > > I'd like to be able to pull out two items at a time - simple examples would > be: > Create this output: > 1 2 > 3 4 > 5 6 > > Create this

Re: Iterate through list two items at a time

2007-01-02 Thread Dave Dean
Thanks for all the fast responses. I'm particularly a fan of the zip method, followed closely by the xrange example. All, of course, are a lot of help! Thanks, Dave -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterate through list two items at a time

2007-01-02 Thread Jeffrey Froman
Dave Dean wrote: > I'm looking for a way to iterate through a list, two (or more) items at a > time. Here's a solution, from the iterools documentation. It may not be the /most/ beautiful, but it is short, and scales well for larger groupings: >>> from itertools import izip >>> def groupn(itera

Re: Iterate through list two items at a time

2007-01-02 Thread Gabriel Genellina
At Tuesday 2/1/2007 22:57, Dave Dean wrote: myList = [1,2,3,4,5,6] I'd like to be able to pull out two items at a time - simple examples would be: Create this output: 1 2 3 4 5 6 b=iter(a) for x in b: y=b.next() print x,y b=iter(a) for x,y in ((item, b.next()) for item in b): prin

Re: Iterate through list two items at a time

2007-01-02 Thread skip
>> I'm looking for a way to iterate through a list, two (or more) items >> at a time. Basically... >> >> myList = [1,2,3,4,5,6] >> >> I'd like to be able to pull out two items at a time... Dan> def pair_list(list_): Dan> return [list_[i:i+2] for i in xrange(

Re: Iterate through list two items at a time

2007-01-02 Thread bearophileHUGS
Few alternative solutions (other are possible), I usually use a variant of the first version, inside a partition function, the second variant is shorter when you don't have a handy partition() function and you don't want to import modules, and the forth one needs less memory when the data is very l

Re: Iterate through list two items at a time

2007-01-02 Thread Dan Bishop
On Jan 2, 7:57 pm, "Dave Dean" <[EMAIL PROTECTED]> wrote: > Hi all, > I'm looking for a way to iterate through a list, two (or more) items at a > time. Basically... > > myList = [1,2,3,4,5,6] > > I'd like to be able to pull out two items at a time... def pair_list(list_): return [list_[i: