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
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 !=
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?
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
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
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
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
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, ...]}
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
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
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, ...
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
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
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
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
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
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
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
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
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
20 matches
Mail list logo