views:

125

answers:

8
+1  A: 

Works just fine for me...

>>> data = [
... [5,3,0,0,7,0,0,0,0],
... [6,0,0,1,9,5,0,0,0],
... [0,9,8,0,0,0,0,6,0],
... [8,0,0,0,6,0,0,0,3],
... [4,0,0,8,0,3,0,0,1],
... [7,0,0,0,2,0,0,0,6],
... [0,6,0,0,0,0,2,8,0],
... [0,0,0,4,1,9,0,0,5],
... [0,0,0,0,8,0,0,7,9]
... ]
>>> element = 4
>>> x = 0
>>> y = 0
>>> print data[0][0]
5
>>> data[x][y] = element
>>> print data[0][0]
4
>>> 
Josh Wright
+1  A: 

You seem to have tabbed out your last line, which gives me an error in the Python interpreter. If I remove that tab, it works.

Your array data has changed. Maybe you aren't printing it out so you don't know that it changed?

Kevin Conner
+1  A: 

What version of python are you running? Can you try it from the command line and post the results, like below? It seems to be working for me. I basically copied and pasted straight from your post.

Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> data = [
... [5,3,0,0,7,0,0,0,0],
... [6,0,0,1,9,5,0,0,0],
... [0,9,8,0,0,0,0,6,0],
... [8,0,0,0,6,0,0,0,3],
... [4,0,0,8,0,3,0,0,1],
... [7,0,0,0,2,0,0,0,6],
... [0,6,0,0,0,0,2,8,0],
... [0,0,0,4,1,9,0,0,5],
... [0,0,0,0,8,0,0,7,9]
... ]
>>> 
>>> element = 4
>>> x = 0
>>> y = 0
>>> 
>>> data
[[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]]
>>> data[x][y] = element
>>> data
[[4, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]]
>>> 
cygnl7
+1  A: 

The only thing wrong I see in your code is that the very last line is at a different indentation level. Putting it at the same level of the rest of the code works fine. :)

You may also be interested in the pprint module:

>>> import pprint
>>> pprint.pprint(data)
[[4, 3, 0, 0, 7, 0, 0, 0, 0],
 [6, 0, 0, 1, 9, 5, 0, 0, 0],
 [0, 9, 8, 0, 0, 0, 0, 6, 0],
 [8, 0, 0, 0, 6, 0, 0, 0, 3],
 [4, 0, 0, 8, 0, 3, 0, 0, 1],
 [7, 0, 0, 0, 2, 0, 0, 0, 6],
 [0, 6, 0, 0, 0, 0, 2, 8, 0],
 [0, 0, 0, 4, 1, 9, 0, 0, 5],
 [0, 0, 0, 0, 8, 0, 0, 7, 9]]

A little easier to read!

Beau
A: 

Ah... now that you've posted all your code, it makes much more sense...

The reason it's not working the way you expected is because 'element' doesn't exist outside the context of the loop. You'll need to store the value you want to use in a variable that exists in the correct scope.

Josh Wright
A: 

It still works with the edited code. I changed the last few lines of your code to this:

print "before =", data[x][y]
print "element =", element
data[x][y] = element 
print "after =", data[x][y]

And those print this:

before = 0
element = 9
after = 9

As has been mentioned, the last value of element from the for loop will be what its value is after the for loop is complete. That's why you get 9 here.

cygnl7
A: 

for loops in Python do not create a new scope; the name you use to hold the current value in the loop will persist after the loop is done, and will retain the last value it had while the loop was running.

Ignacio Vazquez-Abrams
+1  A: 

You need to break for look as soon as you find the necessary element:

item = [1,2,3,4,5,6,7,8,9]
for element in item:
    if element not in z:
            print element
            break

data[x][y] = element 
print data[x][y]
Antony Hatchkins