开发者

Python: Replacing item in a list of lists

Heres my code:

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[x][y] = element

I want to replace the element at coordinates 0,0 but when i print data it hasnt changed the element.


*******EDIT******: OK SO HERES MY FULL CODE:**

data = [
[开发者_StackOverflow中文版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]
]

z = []

#row 6
x1 = 6
for y in range(9):
  print data[x1][y]
  z.append(data[x1][y])


#column 8
y1 = 8 
for x in range(9):
  print data[x][y1]
  z.append(data[x][y1])


#finds the block coordinates
x = 6
y = 8
basex = x - x%3
basey = y - y%3
for x1 in range(basex,basex+3):
    for y1 in range(basey,basey+3):
        print x1,y1, data[x1][y1]
        z.append(data[x1][y1])

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

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


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]


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


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?


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


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:

>>> from pprint import 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!


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜