Re: Pythonic infinite for loop?

2011-04-15 Thread Paul Rubin
Chris Angelico ros...@gmail.com writes: That loop will exit at the first gap in the sequence. If that's what you want, you could try (untested): from itertools import takewhile seq = takewhile(lambda n: ('Keyword%d'%n) in dct, count(1)) lst = map(dct.get, seq) This does 2 lookups per

Re: Pythonic infinite for loop?

2011-04-15 Thread Chris Angelico
On Fri, Apr 15, 2011 at 5:24 PM, Paul Rubin no.email@nospam.invalid wrote: This does 2 lookups per key, which you could avoid by making the code uglier (untested):   sentinel = object()   seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1))   lst = list(takewhile(lambda x: x !=

Re: Pythonic infinite for loop?

2011-04-15 Thread Paul Rubin
Chris Angelico ros...@gmail.com writes:   sentinel = object()   seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1))   lst = list(takewhile(lambda x: x != sentinel, seq)) If I understand this code correctly, that's creating generators, right? It won't evaluate past the sentinel at all?

Re: Pythonic infinite for loop?

2011-04-15 Thread Peter Otten
Paul Rubin wrote: Chris Angelico ros...@gmail.com writes: sentinel = object() seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1)) lst = list(takewhile(lambda x: x != sentinel, seq)) If I understand this code correctly, that's creating generators, right? It won't evaluate past the

Re: Pythonic infinite for loop?

2011-04-15 Thread Peter Otten
Chris Angelico wrote: Apologies for interrupting the vital off-topic discussion, but I have a real Python question to ask. I'm doing something that needs to scan a dictionary for elements that have a particular beginning and a numeric tail, and turn them into a single list with some

Re: Pythonic infinite for loop?

2011-04-15 Thread Chris Angelico
On Fri, Apr 15, 2011 at 6:25 PM, Peter Otten __pete...@web.de wrote: The initial data structure seems less than ideal. You might be able to replace it with a dictionary like {Keyword: [value_for_keyword_1, value_for_keyword_2, ...]} if you try hard enough. The initial data structure comes

Re: Pythonic infinite for loop?

2011-04-15 Thread Peter Otten
Chris Angelico wrote: On Fri, Apr 15, 2011 at 6:25 PM, Peter Otten __pete...@web.de wrote: The initial data structure seems less than ideal. You might be able to replace it with a dictionary like {Keyword: [value_for_keyword_1, value_for_keyword_2, ...]} if you try hard enough. The

Re: Pythonic infinite for loop?

2011-04-15 Thread Steven D'Aprano
On Fri, 15 Apr 2011 18:32:23 +1000, Chris Angelico wrote: On Fri, Apr 15, 2011 at 6:25 PM, Peter Otten __pete...@web.de wrote: The initial data structure seems less than ideal. You might be able to replace it with a dictionary like {Keyword: [value_for_keyword_1, value_for_keyword_2, ...]}

Re: Pythonic infinite for loop?

2011-04-15 Thread Steven D'Aprano
On Fri, 15 Apr 2011 13:58:22 +1000, Chris Angelico wrote: The dictionary is potentially a lot larger than this particular set of values (it's a mapping of header:value for a row of a user-provided CSV file). Does this make a difference to the best option? (Currently I'm looking at likely

Re: Pythonic infinite for loop?

2011-04-15 Thread Roy Smith
In article 4da83f8f$0$29986$c3e8da3$54964...@news.astraweb.com, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: for key in dct: if key.startswith(Keyword): maxkey = max(maxkey, int(key[7:])) I would make that a little easier to read, and less prone to Did I count

Re: Pythonic infinite for loop?

2011-04-15 Thread Ethan Furman
Chris Angelico wrote: lst=[] for i in xrange(1,1000): # arbitrary top, don't like this try: lst.append(parse_kwdlist(dct[Keyword%d%i])) except KeyError: break Possibly overkill: import dbf table = dbf.from_csv(csvfile) # fields get names f0, f1, f2, ...

Re: Pythonic infinite for loop?

2011-04-15 Thread Chris Angelico
On Fri, Apr 15, 2011 at 10:52 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Fri, 15 Apr 2011 13:58:22 +1000, Chris Angelico wrote: The dictionary is potentially a lot larger than this particular set of values (it's a mapping of header:value for a row of a user-provided CSV

Re: Pythonic infinite for loop?

2011-04-15 Thread Paul Rubin
Chris Angelico ros...@gmail.com writes: This whole code is inside a loop that we took, in smoke testing, to a couple hundred million rows (I think), with the intention of having no limit at all. So this might only look at 60-100 headers, but it will be doing so in a tight loop. If you're

Pythonic infinite for loop?

2011-04-14 Thread Chris Angelico
Apologies for interrupting the vital off-topic discussion, but I have a real Python question to ask. I'm doing something that needs to scan a dictionary for elements that have a particular beginning and a numeric tail, and turn them into a single list with some processing. I have a function

Re: Pythonic infinite for loop?

2011-04-14 Thread Ryan Kelly
On Fri, 2011-04-15 at 12:10 +1000, Chris Angelico wrote: Apologies for interrupting the vital off-topic discussion, but I have a real Python question to ask. I'm doing something that needs to scan a dictionary for elements that have a particular beginning and a numeric tail, and turn them

Re: Pythonic infinite for loop?

2011-04-14 Thread Nobody
On Fri, 15 Apr 2011 12:10:52 +1000, Chris Angelico wrote: One, is there a way to make an xrange object and leave the top off? itertools.count() And two, can the entire thing be turned into a list comprehension or something? Generally any construct with a for loop that appends to a list is

Re: Pythonic infinite for loop?

2011-04-14 Thread Steven D'Aprano
On Fri, 15 Apr 2011 12:10:52 +1000, Chris Angelico wrote: Apologies for interrupting the vital off-topic discussion, but I have a real Python question to ask. Sorry, you'll in the wrong forum for that. *wink* [...] My first draft looks something like this. The input dictionary is called

Re: Pythonic infinite for loop?

2011-04-14 Thread John Connor
If I understand your question correctly, what you want is probably something like: i = 0 lst=[] while True: try: lst.append(parse_kwdlist(dct[Keyword%d%i])) i += 1 except KeyError: break --jac On Thu, Apr 14, 2011 at 9:10 PM, Chris Angelico ros...@gmail.com wrote: Apologies for

Re: Pythonic infinite for loop?

2011-04-14 Thread Ryan Kelly
On Fri, 2011-04-15 at 12:34 +1000, Ryan Kelly wrote: On Fri, 2011-04-15 at 12:10 +1000, Chris Angelico wrote: My first draft looks something like this. The input dictionary is called dct, the output list is lst. lst=[] for i in xrange(1,1000): # arbitrary top, don't like this

Re: Pythonic infinite for loop?

2011-04-14 Thread Chris Angelico
Thanks for the responses, all! In its strictest sense, itertools.count() seems to be what I'm after, but may not be what I need. On Fri, Apr 15, 2011 at 12:33 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: No. But you can use an itertools.count([start=0]) object, and then catch