Re: iterator question

2009-04-02 Thread Aaron Brady
On Apr 2, 5:17 pm, grocery_stocker wrote: > On Apr 2, 3:14 pm, grocery_stocker wrote: > > Discussion subject changed to "iterator question" by grocery_stocker Well, I thought it was funny. 'iteratoration'. Next, conversateration? -- http://mail.python.org/mailman/listinfo/python-list

Re: iterator question

2009-04-02 Thread Erik Max Francis
grocery_stocker wrote: How come when I call some_func().next(), the counter doesn't get incremented? Because you're creating a new instance each time you call it. Each new instance starts with 0. But when I call iterator.next(), it does. That's because you're iterating over a single obje

iterator question

2009-04-02 Thread grocery_stocker
On Apr 2, 3:14 pm, grocery_stocker wrote: > Give the following code.. > > >>> class it: > > ...def __init__(self): > ...self.count = -1 > ...def next(self): > ...self.count +=1 > ...if self.count < 4: > ...return self.count > ...else: > ...

Re: simple iterator question

2009-04-02 Thread Arnaud Delobelle
Neal Becker writes: > How do I interleave 2 sequences into a single sequence? Here's a way: >>> a = [1,2,3,4] >>> b = [5,6,7,8] >>> [x for S in zip(a, b) for x in S] [1, 5, 2, 6, 3, 7, 4, 8] > How do I interleave N sequences into a single sequence? In the same way as above. -- Arnaud -- htt

Re: simple iterator question

2009-04-02 Thread andrew cooke
Sion Arrowsmith wrote: > Neal Becker wrote: >>How do I interleave 2 sequences into a single sequence? >> >>How do I interleave N sequences into a single sequence? > > itertools.chain(*itertools.izip(*Nsequences)) aha! thanks. andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: simple iterator question

2009-04-02 Thread Sion Arrowsmith
Neal Becker wrote: >How do I interleave 2 sequences into a single sequence? > >How do I interleave N sequences into a single sequence? itertools.chain(*itertools.izip(*Nsequences)) -- \S under construction -- http://mail.python.org/mailman/listinfo/python-list

Re: simple iterator question

2009-04-02 Thread John O'Hagan
On Thu, 2 Apr 2009, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? Here's one way: def interleave(*args): for n in range(min(len(i) for i in args)) : for i in args: yield i[n] HTH,

Re: simple iterator question

2009-04-02 Thread KDr2
>>> def xl(a): ... return list(reduce(lambda x,y:x+y,zip(*a))) ... >>> xl([[1,2],[3,4]]) [1, 3, 2, 4] like this? Best Regards, -- KDr2, at x-macro.com. On Thu, Apr 2, 2009 at 8:32 PM, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N

Re: simple iterator question

2009-04-02 Thread andrew cooke
Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? don't know if there's a better way, but from itertools import izip def interleave(*doodahs): for together in izip(*doodahs): for single in together:

Re: simple iterator question

2009-04-02 Thread George Sakkis
On Apr 2, 8:32 am, Neal Becker wrote: > How do I interleave 2 sequences into a single sequence? > > How do I interleave N sequences into a single sequence? http://lmgtfy.com/?q=python+interleave+sequences http://code.activestate.com/recipes/511480/ http://code.activestate.com/recipes/528936/ HT

simple iterator question

2009-04-02 Thread Neal Becker
How do I interleave 2 sequences into a single sequence? How do I interleave N sequences into a single sequence? -- http://mail.python.org/mailman/listinfo/python-list

Re: csv iterator question

2008-05-24 Thread David Jackson
Thanks, Ethan. that was a great solution. i just tested it. On Fri, May 23, 2008 at 7:08 PM, Ethan Furman <[EMAIL PROTECTED]> wrote: > davidj411 wrote: > > When you save an open file to a variable, you can re-use that variable >> for membership checking. >> it does not seem to be that way with

Re: csv iterator question

2008-05-23 Thread alex23
On May 24, 6:36 am, davidj411 <[EMAIL PROTECTED]> wrote: > but when i try to iterate through it again, it appears to print > nothing (no error either). the file is exhausted? No, the iterator is finished. Iterators are generally use-once: "The intention of the protocol is that once an iterator's

Re: csv iterator question

2008-05-23 Thread Marc 'BlackJack' Rintsch
On Fri, 23 May 2008 13:36:55 -0700, davidj411 wrote: > example of open method on file object: > fhandle=open(filename).readelines() Here the name `fhandle` is somewhat misleading. You don't bind the file object to that name, but the result of the call of the `readlines()` method. Which is a lis

Re: csv iterator question

2008-05-23 Thread Ethan Furman
davidj411 wrote: When you save an open file to a variable, you can re-use that variable for membership checking. it does not seem to be that way with the csv.reader function, even when set to a variable name. what is the best way to store the open CSV file in memory or do i need to open the fil

Re: csv iterator question

2008-05-23 Thread Mike Driscoll
On May 23, 3:36 pm, davidj411 <[EMAIL PROTECTED]> wrote: > When you save an open file to a variable, you can re-use that variable > for membership checking. > it does not seem to be that way with the csv.reader function, even > when set to a variable name. > > what is the best way to store the open

csv iterator question

2008-05-23 Thread davidj411
When you save an open file to a variable, you can re-use that variable for membership checking. it does not seem to be that way with the csv.reader function, even when set to a variable name. what is the best way to store the open CSV file in memory or do i need to open the file each time? exampl

Re: Beautiful Soup iterator question....

2007-04-20 Thread Paul McGuire
On Apr 20, 2:05 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > > did you try something like (untested) > > cell1, cell2, cell3, cell4, cell5, \ > cell6, cell7, cell8 = row.findAll("td") > > No need for the "for" if you want to handle each cell differently, you > won;t be iterating o

Beautiful Soup iterator question....

2007-04-20 Thread cjl
P: I am screen-scraping a table. The table has an unknown number of rows, but each row has exactly 8 cells. I would like to extract the data from the cells, but the first three cells in each row have their data nested inside other tags. So I have the following code: for row in table.findAll("tr

Re: Beautiful Soup iterator question....

2007-04-20 Thread Steve Holden
cjl wrote: > P: > > I am screen-scraping a table. The table has an unknown number of rows, > but each row has exactly 8 cells. I would like to extract the data > from the cells, but the first three cells in each row have their data > nested inside other tags. > > So I have the following code: >

Re: iterator question

2006-09-29 Thread Travis Oliphant
Neal Becker wrote: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an integer that specifies the size of tuple > to retu

Re: iterator question

2006-09-27 Thread [EMAIL PROTECTED]
Er, whoops. That would work if the last item in the list was 10 (and, of course, this doesn't work for any arbitrary sequence). Is there any "look-ahead" function for list comprehensions? Danny [EMAIL PROTECTED] wrote: > Simple list comprehension? > > >>> l = [1,2,3,4,5,6,7,8,9,0] > >>> [(x, x+

Re: iterator question

2006-09-27 Thread [EMAIL PROTECTED]
Simple list comprehension? >>> l = [1,2,3,4,5,6,7,8,9,0] >>> [(x, x+1) for x in l if x%2 == 1] [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)] Danny Neal Becker wrote: > George Sakkis wrote: > > > [EMAIL PROTECTED] wrote: > > > >> def transform(seq, size): > >> i = 0 > >> while i < len(seq): >

Re: iterator question

2006-09-27 Thread Neal Becker
George Sakkis wrote: > [EMAIL PROTECTED] wrote: > >> def transform(seq, size): >> i = 0 >> while i < len(seq): >> yield tuple(seq[i:i+size]) >> i += size > > Or for arbitrary iterables, not just sequences: > > from itertools import islice > def transform(iterable, size):

Re: iterator question

2006-09-26 Thread George Sakkis
Steve Holden wrote: > George Sakkis wrote: > > Neil Cerutti wrote: > > > > > >>On 2006-09-26, Neal Becker <[EMAIL PROTECTED]> wrote: > >> > >>>Any suggestions for transforming the sequence: > >>> > >>>[1, 2, 3, 4...] > >>>Where 1,2,3.. are it the ith item in an arbitrary sequence > >>> > >>>into a

Re: iterator question

2006-09-26 Thread Rob Williscroft
Rob Williscroft wrote in news:Xns984ACDA635C9rtwfreenetREMOVEcouk@ 216.196.109.145 in comp.lang.python: seq = range(11) zip(seq[::2], seq[1::2] + [None]) > [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)] > seq = range(10) zip(seq[::2], seq[1::2] + [None]) > [(0, 1), (2, 3

Re: iterator question

2006-09-26 Thread Rob Williscroft
Neal Becker wrote in news:[EMAIL PROTECTED] in comp.lang.python: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an in

Re: iterator question

2006-09-26 Thread Bruno Desthuilliers
Neal Becker a écrit : > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an integer that specifies the size of tuple > to r

Re: iterator question

2006-09-26 Thread Steve Holden
George Sakkis wrote: > Neil Cerutti wrote: > > >>On 2006-09-26, Neal Becker <[EMAIL PROTECTED]> wrote: >> >>>Any suggestions for transforming the sequence: >>> >>>[1, 2, 3, 4...] >>>Where 1,2,3.. are it the ith item in an arbitrary sequence >>> >>>into a succession of tuples: >>> >>>[(1, 2), (3,

Re: iterator question

2006-09-26 Thread Boris Borcic
Neal Becker wrote: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an integer that specifies the size of tuple > to retu

Re: iterator question

2006-09-26 Thread George Sakkis
Neil Cerutti wrote: > On 2006-09-26, Neal Becker <[EMAIL PROTECTED]> wrote: > > Any suggestions for transforming the sequence: > > > > [1, 2, 3, 4...] > > Where 1,2,3.. are it the ith item in an arbitrary sequence > > > > into a succession of tuples: > > > > [(1, 2), (3, 4)...] > > > > In other wo

Re: iterator question

2006-09-26 Thread Neil Cerutti
On 2006-09-26, Neal Becker <[EMAIL PROTECTED]> wrote: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an integer that specif

Re: iterator question

2006-09-26 Thread George Sakkis
[EMAIL PROTECTED] wrote: > def transform(seq, size): > i = 0 > while i < len(seq): > yield tuple(seq[i:i+size]) > i += size Or for arbitrary iterables, not just sequences: from itertools import islice def transform(iterable, size): it = iter(iterable) while True

Re: iterator question

2006-09-26 Thread John Hicken
Neal Becker wrote: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > > [(1, 2), (3, 4)...] > > In other words, given a seq and an integer that specifies the size of tuple > to return,

Re: iterator question

2006-09-26 Thread johnzenger
def transform(seq, size): i = 0 while i < len(seq): yield tuple(seq[i:i+size]) i += size Neal Becker wrote: > Any suggestions for transforming the sequence: > > [1, 2, 3, 4...] > Where 1,2,3.. are it the ith item in an arbitrary sequence > > into a succession of tuples: > >

iterator question

2006-09-26 Thread Neal Becker
Any suggestions for transforming the sequence: [1, 2, 3, 4...] Where 1,2,3.. are it the ith item in an arbitrary sequence into a succession of tuples: [(1, 2), (3, 4)...] In other words, given a seq and an integer that specifies the size of tuple to return, then for example: seq = [a,b,c,d,e,f