473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Looping over a list question

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
6 1178
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JP SIngh | last post by:
Hi All I have threee tables like the one below and i would like to ask if it is possible to display the data from all three tables using a single recordset in ASP. TABLE - tblList id -...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
4
by: Norman Fritag | last post by:
Hi there, >>>__ 1020.83, 2305.22, 1176.86, 755.12, 123.41 __ 1976.1, 1325.99, 947, 718.03, 414.32 __ 1020.83, 1976.1, 352.5, 947, 718.03, 366.98 Their IDs were as...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
2
by: pob | last post by:
Whats the difference between using a control or a listbox when looping thru a listbox. In example 1 it dims a listbox and an example 2 it dims a control. Please explain. Thanks in advance ...
67
by: gator | last post by:
I am quite new to XML and posted a request for example code yesterday. Unfortunately, I did not do a very good job in explaining what I was looking for. Here is an example of a small piece of the...
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
10
by: Charlie Brown | last post by:
I am using something like the following function to loop through a recipe program I am working on. As I was writing it, I starting thinking this isn't going to work at all... but not I'm not so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.