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
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)
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
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
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
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
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
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
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
>> 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(
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
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:
12 matches
Mail list logo