Re: [Tutor] For loop breaking string methods

2010-04-27 Thread Lie Ryan
On 04/27/10 12:19, Dave Angel wrote:
 Note also that if you insert or delete from the list while you're
 looping, you can get undefined results.  That's one reason it's common
 to build a new loop, and just assign it back when done.  Example would
 be the list comprehension you showed earlier.


I have to add to Dave's statement, if you modify the list's content
while looping there is no undefined behavior; you get undefined behavior
if you modify the list's structure.

Operations that modify a list's structure includes insertion, delete,
append, etc; those operations which can modify the len() of list.

Modifying the content, however, is perfectly safe.

However, even when just modifying list's content, I personally still
prefer list/generator comprehension. They're fast and simple.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-27 Thread bob gailer

On 4/26/2010 10:19 PM, Dave Angel wrote:


Note also that if you insert or delete from the list while you're 
looping, you can get undefined results.




The results are not undefined. They m,ight be unexpected, but that is 
due to an invalid assumption.


The behavior is explained in section 7.3 of The Python Language 
Reference. Note especially:




There is a subtlety when the sequence is being modified by the loop 
(this can only occur for mutable sequences, i.e. lists). An internal 
counter is used to keep track of which item is used next, and this is 
incremented on each iteration. When this counter has reached the length 
of the sequence the loop terminates. This means that if the suite 
deletes the current (or a previous) item from the sequence, the next 
item will be skipped (since it gets the index of the current item which 
has already been treated). Likewise, if the suite inserts an item in the 
sequence before the current item, the current item will be treated again 
the next time through the loop. This can lead to nasty bugs that can be 
avoided by making a temporary copy using a slice of the whole sequence, 
e.g.,


for  x  in  a[:]:
if  x0:  a.remove(x)



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread bob gailer

On 4/26/2010 3:38 PM, C M Caine wrote:

Why does this not work:


By work you mean do what I want it to do.


 L = [' foo ','bar ']
 for i in L:
i = i.strip()


This creates a new local variable named i. It does not affect L. This 
has nothing to do with loops nor is it strange behavior - it is expected 
behavior.


Consider the loopless equivalent:

 i = L[0]
 i = i.strip()
 L
[' foo ', 'bar ']




 L
[' foo ', 'bar ']
 # note the leading whitespace that has not been removed.

But this does:
 L = [i.strip() for i in L]
 L
['foo', 'bar']

What other strange behaviour should I expect from for loops?



None - loops do not have strange behaviors.
Perhaps unexpected but that is a result of not understanding an aspect 
of the language.


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
Thank you for the clarification, bob.

For any future readers of this thread I include this link[1] to effbot's
guide on lists, which I probably should have already read.

My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
L[0] = some_func(value)

Is this the standard method?

[1]: http://effbot.org/zone/python-list.htm

Colin Caine
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Sander Sweers
On 26 April 2010 21:38, C M Caine cmca...@googlemail.com wrote:
 Why does this not work:
 L = [' foo ','bar ']
 for i in L:
     i = i.strip()

str.strip() _returns_ a *new* string and leaves the original string
alone. The reason being that string are immutable so can not be
changed.

 s1 = ' foo '
 s1[1]
'f'
 s1[1] = 'g'

Traceback (most recent call last):
  File pyshell#7, line 1, in module
s1[1] = 'g'
TypeError: 'str' object does not support item assignment

However lists are mutable and can be changed in place.

 l = [' foo ','bar ']
 l[0] = ' boo '
 l
[' boo ', 'bar ']
 l[1] = 'far '
 l
[' boo ', 'far ']


 But this does:
 L = [i.strip() for i in L]
 L
 ['foo', 'bar']

What you do here is create a *new* list object and assign it to
variable L. The new list is created with *new* string objects returned
by str.strip().

Putting the 2 together you could do something like below but I would
use a list comprehension like you did above:

 l = [' foo ','bar ']
 for x in range(len(l)):
l[x] = l[x].strip()

 l
['foo', 'bar']


 What other strange behaviour should I expect from for loops?

You should read up on immutable data types like strings and tuples.
Start with [1].

Greets
Sander

[1] http://docs.python.org/reference/datamodel.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Alan Gauld


C M Caine cmca...@googlemail.com wrote 


My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
   L[0] = some_func(value)


I think you mean:
 L[index] = some_func(value)


Is this the standard method?


Or use a List copmprehension.

L = [some_func(value) for value in L]

I'd say nowadays that the comprehension was most common.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
 What other strange behaviour should I expect from for loops?

 You should read up on immutable data types like strings and tuples.
 Start with [1].

 Greets
 Sander

 [1] http://docs.python.org/reference/datamodel.html


Thank you kindly for your reply, I'll be sure to read up on it.

Colin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
On 26 April 2010 23:45, Alan Gauld alan.ga...@btinternet.com wrote:

 C M Caine cmca...@googlemail.com wrote

 My intention now is to modify list contents in the following fashion:

 for index, value in enumerate(L):
   L[0] = some_func(value)

 I think you mean:
     L[index] = some_func(value)

Yes, I do

 Is this the standard method?

 Or use a List copmprehension.

 L = [some_func(value) for value in L]

 I'd say nowadays that the comprehension was most common.

 --
 Alan Gauld
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/

Thanks, I can see why the comprehensions are more popular.

Colin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Dave Angel

C M Caine wrote:

Thank you for the clarification, bob.

For any future readers of this thread I include this link[1] to effbot's
guide on lists, which I probably should have already read.

My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
L[0] = some_func(value)

Is this the standard method?

[1]: http://effbot.org/zone/python-list.htm

Colin Caine

  

Almost.   You should have said

   L[index] = some_func(value)

The way you had it, it would only replace the zeroth item of the list.

Note also that if you insert or delete from the list while you're 
looping, you can get undefined results.  That's one reason it's common 
to build a new loop, and just assign it back when done.  Example would 
be the list comprehension you showed earlier.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor