继承我的代码:
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
我想替换坐标0,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]
]
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]
答案 0 :(得分:1)
对我来说效果很好......
>>> 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
>>>
答案 1 :(得分:1)
你好像已经标出了你的最后一行,这在Python解释器中给了我一个错误。如果我删除该标签,它就可以。
您的数组data
已更改。也许你不打印它,所以你不知道它改变了吗?
答案 2 :(得分:1)
你在运行什么版本的python?你可以从命令行试试并发布结果,如下所示?它似乎对我有用。我基本上是直接复制并粘贴你的帖子。
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]]
>>>
答案 3 :(得分:1)
我在代码中看到的唯一错误是最后一行是在不同的缩进级别。将它放在与其余代码相同的级别上工作正常。 :)
您可能也对pprint模块感兴趣:
>>> 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]]
更容易阅读!
答案 4 :(得分:1)
一旦找到必要的元素,你需要打破外观:
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]
答案 5 :(得分:0)
啊......现在您已经发布了所有代码,它更有意义......
它没有按照你预期的方式工作的原因是因为'element'在循环的上下文之外不存在。您需要将要使用的值存储在存在于正确范围内的变量中。
答案 6 :(得分:0)
它仍适用于编辑过的代码。我将代码的最后几行更改为:
print "before =", data[x][y]
print "element =", element
data[x][y] = element
print "after =", data[x][y]
那些打印出来:
before = 0
element = 9
after = 9
如前所述,for循环中元素的最后一个值将是for循环完成后的值。这就是你在这里得到9的原因。
答案 7 :(得分:0)
for
循环不会创建新范围;用于在循环中保存当前值的名称将在循环完成后保留,并将保留循环运行时的最后一个值。