简体   繁体   English

Python:替换列表中的项目

[英]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. 我想在坐标0,0处替换元素,但是当我打印数据时,它并没有更改元素。


*******EDIT******: OK SO HERES MY FULL 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]
]

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]

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. 您似乎已经选择了最后一行,这给我Python解释器一个错误。 If I remove that tab, it works. 如果我删除该标签,它将起作用。

Your array data has changed. 您的阵列data已更改。 Maybe you aren't printing it out so you don't know that it changed? 也许您没有打印出来,所以您不知道它已更改?

What version of python are you running? 您正在运行什么版本的python? 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: 您可能也对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]]

A little easier to read! 更容易阅读!

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]

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. 您需要将要使用的值存储在正确范围内的变量中。

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. 如前所述,for循环中element的最后一个值将是for循环完成后其值。 That's why you get 9 here. 这就是为什么你在这里得到9。

for loops in Python do not create a new scope; Python中的for循环不会创建新的作用域; 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. 用于在循环中保存当前值的名称将在循环完成后保留,并保留循环运行时拥有的最后一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM