Re: a sequence question

2005-02-12 Thread Nick Coghlan
David Isaac wrote: "Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] A bug report on Sourceforge would help in getting the problem fixed for the 2.5 docs Done. Bug 1121416, for anyone else interested. Looks Raymond agrees with me about the left-to-right evaluation of iter

Re: a sequence question

2005-02-12 Thread David Isaac
"Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > A bug report on Sourceforge would help in getting the problem fixed for the 2.5 > docs Done. > For the 'left-to-right' evaluation thing, that's technically an implementation > artifact of the CPython implementation, si

Re: a sequence question

2005-02-12 Thread Nick Coghlan
David Isaac wrote: If that is right, I still cannot extract it from the doc cited above. So where should I have looked? Ouch. The terminology's evolved, and it looks to me like the docs for the older builtins haven't been updated to track it. The terminology has pretty much settled to 'iterable'

Re: a sequence question

2005-02-11 Thread David Isaac
> Alan Isaac wrote: > > I see that [iter(l)]*N produces an N element list with each element being > > the same iterator object, but after that > > http://www.python.org/doc/2.3.5/lib/built-in-funcs.html > > just didn't get me there. "Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL P

Re: a sequence question

2005-02-11 Thread Nick Coghlan
David Isaac wrote: "Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Using zip(*[iter(l)]*N) or zip(*(iter(l),)*N) simply extends the above to the general case. Clearly true. But can you please go into much more detail for a newbie? I see that [iter(l)]*N produces an N ele

Re: a sequence question

2005-02-10 Thread David Isaac
"Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Using zip(*[iter(l)]*N) or zip(*(iter(l),)*N) simply extends the above to the > general case. Clearly true. But can you please go into much more detail for a newbie? I see that [iter(l)]*N produces an N element list with

Re: a sequence question

2005-02-01 Thread Nick Coghlan
Steven Bethard wrote: I think you can write that second one so that it works for iterables without a __len__: py> def padded_partition(iterable, part_len, pad_val=None): ... itr = itertools.chain( ... iter(iterable), itertools.repeat(pad_val, part_len - 1)) ... return itertools.iz

Re: a sequence question

2005-02-01 Thread todddeluca
Chris Wright wrote: > Hi, > > 1) I want to iterate over a list "N at a time" > sort of like: > > # Two at a time... won't work, obviously > > >>> for a, b in [1,2,3,4]: > ... print a,b > ... > Traceback (most recent call last): >File "", line 1, in ? > TypeError: unpack non-sequence > >>>

Re: a sequence question

2005-01-31 Thread Steven Bethard
Nick Coghlan wrote: 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=N

Re: a sequence question

2005-01-30 Thread gene . tani
cookbook's not an easy grep but: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303060 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347689 -- http://mail.python.org/mailman/listinfo/python-list

Re: a sequence question

2005-01-28 Thread Nick Coghlan
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 el

Re: a sequence question

2005-01-28 Thread Michael Hartl
For problems like this I use a partition function defined in a utils.py file that I use (based on Peter Norvig's utils file at http://aima.cs.berkeley.edu/python/utils.py). Using partition, the problem you posed can be solved by writing #for a, b in partition([1, 2, 3, 4], 2): #print a, b Th

Re: a sequence question

2005-01-28 Thread Duncan Booth
Chris Wright wrote: > 1) I want to iterate over a list "N at a time" > sort of like: > > # Two at a time... won't work, obviously > > >>> for a, b in [1,2,3,4]: > ... print a,b > ... Try this: l = [1, 2, 3, 4] for a, b in zip(*[iter(l)]*2): print a, b zip(*[iter(seq)]*N) will gr

Re: a sequence question

2005-01-28 Thread F. Petitjean
Le Fri, 28 Jan 2005 13:59:45 GMT, Chris Wright a écrit : > Hi, > > 1) I want to iterate over a list "N at a time" > > > Is there a nifty way to do with with list comprehensions, > or do I just have to loop over the list ? > > cheers and thanks seq = xrange(1, 9) # an iterable [1, 2, ... 8] N

Re: a sequence question

2005-01-28 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Chris Wright <[EMAIL PROTECTED]> wrote: > Hi, > > 1) I want to iterate over a list "N at a time" You could do it with slicing and zip: >>> l = [1, 2, 3, 4, 5, 6, 7, 8] >>> zip (l[::2], l[1::2]) [(1, 2), (3, 4), (5, 6), (7, 8)] To my eyes, that's a bit cryptic,

Re: a sequence question

2005-01-28 Thread Diez B. Roggisch
l = [1,2,3,4] for a, b in zip(l[::2], l[1::2]): print a,b -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list