By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

Looping over a list question

P: n/a
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

Oct 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tuesday 03 October 2006 19:50, st*****@theboulets.net wrote:
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
Oct 3 '06 #2

P: n/a
st*****@theboulets.net wrote:
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>

Oct 3 '06 #3

P: n/a

st*****@theboulets.net wrote:
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

Yes, there has:
http://groups.google.ca/group/comp.l...af21b68309415f

Oct 3 '06 #4

P: n/a
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.

md******@gmail.com wrote:
Yes, there has:
http://groups.google.ca/group/comp.l...hread/thread/9...
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

Oct 3 '06 #5

P: n/a
st*****@theboulets.net wrote:
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>

Oct 3 '06 #6

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


Oct 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.