Re: [Tutor] Removing lines in string-table

2005-05-17 Thread Olli Rajala
My code:
> > for line in fileNames:
> > if line[-10:] == '_thumb.jpg':
> > fileNames.remove(line)

Chris wrote:
> 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.  

Oh, yeah, that's right. I just didn't notice it... Thanks for
correcting me! Actually it wouldn't have mattered (I think) because
the list contains x.jpg and x_thumb.jpg which have been collected by
os.listdir(). At least I suppose that it would be like [x.jpg,
x_thumb.jpg, y.jpg, y_thumb.jpg] or am I completely wrong? But thanks
for good suggestions, I replaced my code with the list comprehension
method and it works now. Thanks!

Yours, 
-- 
Olli Rajala <><
Tampere, Finland
http://www.students.tut.fi/~rajala37/

"In theory, Theory and Practice should be
the same. But in practice, they aren't."
- Murphy's Proverbs
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Removing lines in string-table

2005-05-17 Thread Chris Smith

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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Removing lines in string-table

2005-05-17 Thread Olli Rajala
>  Looks like a job for a list comprehension:
> 
> fileNames = [element for element in fileNames if not element.endswith
> ("_thumb.jpg")]

Thanks Max! It seem to work, but now I have to do some reading,
because I have no idea why it works or what it really does. :) But
thanks anyway.

Yours, 
-- 
Olli Rajala <><
Tampere, Finland
http://www.students.tut.fi/~rajala37/

"In theory, Theory and Practice should be
the same. But in practice, they aren't."
- Murphy's Proverbs
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



Re: [Tutor] Removing lines in string-table

2005-05-17 Thread Max Noel

On May 17, 2005, at 08:52, Olli Rajala wrote:

> Okay,
> I have a string table (don't recall the right word used in Python
> right now)

 It's called a list, or an array.

> 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.

 Looks like a job for a list comprehension:

fileNames = [element for element in fileNames if not element.endswith 
("_thumb.jpg")]


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Removing lines in string-table

2005-05-17 Thread Olli Rajala
Okay,
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.

Back to coding...
-- 
Olli Rajala <><
Tampere, Finland
http://www.students.tut.fi/~rajala37/

"In theory, Theory and Practice should be
the same. But in practice, they aren't."
- Murphy's Proverbs
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor