Looping over a list question

S

stephen

I found myself writing:

for f in [i for i in datafiles if '.txt' in i]:
print 'Processing datafile %s' % f

but I was wishing that I could have instead written:

for f in in datafiles if '.txt' in f:
print 'Processing datafile %s' % f

Has there ever been a proposal for this? Just wondering ...

Stephen Boulet
 
C

Christoph Haas

I found myself writing:

for f in [i for i in datafiles if '.txt' in i]:
print 'Processing datafile %s' % f

but I was wishing that I could have instead written:

for f in in datafiles if '.txt' in f:
print 'Processing datafile %s' % f

What about:


import glob
for filename in glob.glob('*.txt'):
print "Processing file", filename
...


Christoph
 
F

Fredrik Lundh

I found myself writing:

for f in [i for i in datafiles if '.txt' in i]:
print 'Processing datafile %s' % f

but I was wishing that I could have instead written:

for f in in datafiles if '.txt' in f:
print 'Processing datafile %s' % f

Has there ever been a proposal for this?

probably. but unless your editor or keyboard is horribly broken, you
can of course press return at the right place instead:

for i in datafiles:
if '.txt' in i:
print 'Processing datafile %s' % f

(for this specific case, "endswith" is more reliable than "in", btw)

</F>
 
S

stephen

Fredrik Lundh wrote:
[snip]
probably. but unless your editor or keyboard is horribly broken, you
can of course press return at the right place instead:

for i in datafiles:
if '.txt' in i:
print 'Processing datafile %s' % f

(for this specific case, "endswith" is more reliable than "in", btw)

</F>

My "print" line is actually a long 40 line block. I guess I could just
do
datafiles = [f for f in datafiles if f.endswith('.txt')]
and then loop over it.


Thanks for the link. I didn't know about str.startswith and
str.endswith, which I can see is very useful.

So, not a universally popular proposal. Interesting how different
programming 'phrasings' resonate with different people. For me, for
example, lambda causes my eyes to glaze over.

I do love list comprehensions though. I'd also love to see string
constants implemented some day too (like str.whitespace and
str.ascii_letters).

Stephen
 
F

Fredrik Lundh

My "print" line is actually a long 40 line block.

and? if your editor isn't horribly broken, that shouldn't be much
of a problem.

(but you may want to refactor the code; if you have a processing
block that large, it's probably better to move all or parts of it
into a support function).

</F>
 
T

Tim Chase

I'd also love to see string constants implemented some day too
(like str.whitespace and str.ascii_letters).

You mean like the "string" module provides? :)
>>> import string
>>> print '\n'.join(["%s -> %s" % (s, repr(eval('string.%s' %
s))) for s in dir(string) if isinstance(eval('string.%s' % s),
basestring) and not s.startswith('_')])

ascii_letters ->
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase -> 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase -> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits -> '0123456789'
hexdigits -> '0123456789abcdefABCDEF'
letters -> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase -> 'abcdefghijklmnopqrstuvwxyz'
octdigits -> '01234567'
printable ->
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$
%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
punctuation -> '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
uppercase -> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
whitespace -> '\t\n\x0b\x0c\r ''abcdefghijklmnopqrstuvwxyz'

(you mentioned liking list comprehensions, so that ugly hack of a
one-liner extracts all the string properties of the "string"
module that don't begin with an underscore)

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top