On Tuesday, May 17, 2005, at 08:35 America/Chicago,
[EMAIL PROTECTED] wrote:
> I have a string table (don't recall the right word used in Python
> right now) and would like to remove every 'cell' that contains a
> string '_thumb.jpg'. There are 1-> digits before the string if that
> matters. I made a for-loop that does what I want to:
>
> for line in fileNames:
> if line[-10:] == '_thumb.jpg':
> fileNames.remove(line)
>
> But I really doubt that it's not the best way to do this. So, any
> comments are really appreciated.
>
The above will not work if two successive lines contain the target
text. When you remove the one, its neighbor "slides over" to take the
place of the one removed and then when you proceed to the "next" line
you are actually skipping the one that slid over. This could be
remedied with using indices to access the list, but perhaps a better
approach is to use filter or a list comprehension to remove the target
lines:
###
def myfilter(x):
return not x.endswith('_thumb.jpg')
fileNames =filter(myfilter, fileNames)
# OR
fileNames =[x for x in fileNames if not x.endswith('_thumb.jpg')]
###
/c
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor