View Full Version : [Python] Confused by for loops
TreeFinger
July 16th, 2008, 12:39 PM
li = [1, 2, 1, 2, 1, 2]
for i in li:
li[i] = 0
print li
[0, 0, 0, 2, 1, 2]
I thought the following would work..
li = [1, 2, 1, 2, 1, 2]
for i in li:
i = 0
print li
[1, 2, 1, 2, 1, 2]
I'm confused :confused:
themusicwave
July 16th, 2008, 01:03 PM
First off those two loops are totally different.
The python loops runs once for each item in li. What is happening is that you are getting the Value in each of the cells, not it's index.
Try this:
li = [1, 2, 1, 2, 1, 2]
for item in li:
print item
You will see the values print in order like so:
1
2
1
2
1
2
If you want the indexes do this:
index=0
li = [1, 2, 1, 2, 1, 2]
while index < len(li):
print "Value at index #" + str(index) + " " + li[index]
index += 1
That is the fundamental difference. The For in python is very similar to a for each loop in other languages if you are familiar with such a thing.
TreeFinger
July 16th, 2008, 01:04 PM
I'm simply attempting to change all the values in the list to 0.
themusicwave
July 16th, 2008, 01:09 PM
I forgot to look at the the second part.
With your loop:
li = [1, 2, 1, 2, 1, 2]
for i in li:
i = 0
print li
The line i=0 isn't doing anything. Your loop is doing nothing to li. You would get the same result with:
li = [1, 2, 1, 2, 1, 2]
print li
If you wanted to put all zeros in the list this would work:
li = [1,2,1,2,1,2]
count=0
while count < len(li):
li[count]=0
count +=1
print li
That yields: [0, 0, 0, 0, 0, 0]
There may be a better way to do it once the Python gurus weight in.
TreeFinger
July 16th, 2008, 01:14 PM
I can see that i = 0 is doing nothing by the output
This code prints each value
li = [1, 2, 3, 4, 5, 6]
for i in li:
print i
which confuses me why i = 0 does nothing.
tiachopvutru
July 16th, 2008, 01:21 PM
I thought the following would work..
li = [1, 2, 1, 2, 1, 2]
for i in li:
i = 0
print li
[1, 2, 1, 2, 1, 2]
I'm confused :confused:
For this one, until someone comes in who can explains what's truly going on, you will have to stick with what I think the problem is. The i in each iteration simply gets assigned to each item in the list, and in Python, assignment means referencing. A variable is simply a name that points to an object (an object can be a number, a string, etc.). So, think of when the for loop runs the first iteration, i and li[0] points at the same number 1, then you change i to point at a different number, which is 0, but li[0] still point at the same number that it originally pointed, which is 1. At least, that's what I think. =p
Anyway, you can use the while loop in this one, among a whole bunch of others (although most are unnecessary more complicated). You can use the list comprehension, too, which is faster and shorter.
li = [1, 2, 1, 2, 1, 2]
li = [i * 0 for i in li]
Note that I put i * 0 instead of i = 0 inside the bracket [], that's because list comprehension (which is what it is inside the []) only accept expressions and not statements (expressions are like a + b and statements are like a = b).
ghostdog74
July 16th, 2008, 01:29 PM
li = [1, 2, 1, 2, 1, 2]
for i in li:
li[i] = 0
print li
I'm confused :confused:
why are you confused? if you do this, you will know why
for i in li:
print "value of i is ", i
print "value of li[i] is ", li[i]
CptPicard
July 16th, 2008, 01:32 PM
in Python, assignment means referencing.
Yes, the issue is that the only thing here that is being actually assigned is the "i" and not the list contents. Which means that at each iteration, i is first made to refer to an item in the list, and then inside the loop, it is made to refer to a constant 0. Then we just go again...
EDIT: To be more clear of what your misconception is here... if this held what you're trying to do, this would be true:
a = 5
b = a
b = 3
#Now a would be 3 but of course it isn't.
themusicwave
July 16th, 2008, 01:32 PM
I think that tiach is right about the i=0 thing, but I too will wait for some gurus.
As for the first one
li = [1, 2, 1, 2, 1, 2]
for i in li:
li[i] = 0
print li
In that one I know exactly what happens. Let's look at the loop step by step.
1)The loop enters and the first value is pulled from li and assigned to i. This is 1.
Now, li[1] is set to 0. Li now looks like [1,0,1,2,1,2]
2) The loop runs again this next value of li is pulled out which is 0 and this is assigned to i.
Now, li[0] is set to 0. Li looks like [0,0,1,2,1,2].
3) The loop runs again this next value of li is pulled out which is 1 and this is assigned to i.
Now, li[1] is set to 0. Li looks like [0,0,1,2,1,2].
4) The loop runs again this next value of li is pulled out which is 2 and this is assigned to i.
Now, li[2] is set to 0. Li looks like [0,0,0,2,1,2].
5) The loop runs again this next value of li is pulled out which is 1 and this is assigned to i.
Now, li[1] is set to 0. Li looks like [0,0,0,2,1,2].
6) The loop runs again this next value of li is pulled out which is 2 and this is assigned to i.
Now, li[2] is set to 0. Li looks like [0,0,0,2,1,2].
The loop runs once for each value in li. The confusion really comes from the fact that li[1] is set to 0 which then causes li[0] to get set to 0 as well.
ghostdog74
July 16th, 2008, 01:33 PM
I'm simply attempting to change all the values in the list to 0.
use a for loop going over the length of the list
for i in range(len(li)):
li[i]=0
or simply create another list with all 0's according to the length of li.
or
map(lambda x :0 , li)
Martin Witte
July 16th, 2008, 08:09 PM
as others suggested in this thread - create a new list with length of the original list and fill it with zeros, a quick way is
>>> li = [1, 2, 1, 2, 1, 2]
>>> li = [0, ] * len(li)
>>> print li
[0, 0, 0, 0, 0, 0]
>>>
Can+~
July 16th, 2008, 08:39 PM
The iterator "i" in the for loop just copies the value contained in each element of the list. Therefore, modifying the value of i has no effect on the original *list* (why I said array?).
Here's a more "Pythonic" way:
a = [1, 2, 1, 2, 3, 1]
print a
# Define a short function that just returns 0, and map it.
a = map(lambda x: 0, a)
print a
Although the method suggested before is more efficient. The map function makes sense when there are values to compute.
Smygis
July 16th, 2008, 11:17 PM
>>> a = [1, 2, 1, 2, 3, 1]
>>> for i in xrange(len(a)):
... a[i] = 0
...
>>> a
[0, 0, 0, 0, 0, 0]
>>> # or the exact same thing a diffrent way. The most Pythonic way
...
>>> a = [1, 2, 1, 2, 3, 1]
>>> a = [0 for _ in a]
>>> a
[0, 0, 0, 0, 0, 0]
>>>
Lux Perpetua
July 17th, 2008, 04:30 PM
I can see that i = 0 is doing nothing by the output
This code prints each value
li = [1, 2, 3, 4, 5, 6]
for i in li:
print i
which confuses me why i = 0 does nothing.I think I know what's confusing you: the i in the loop body gets its own copy of each element of the list. It doesn't become an alias for the corresponding list element.
To do what you want, I'd probably do something like
for i in xrange(len(li)):
li[i] = 0
dmm1285
July 17th, 2008, 07:27 PM
Another way to do it:
[0 for i in li]
Smygis
July 17th, 2008, 07:42 PM
how nice that the two guys behind me wrote the exact same thing i did.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.