首页 > 解决方案 > Python更改字段的第一个值

问题描述

我尝试将python中字段的第一个值更改为true。我究竟做错了什么?整个专栏都发生了变化,我不明白为什么。

我的代码:

def selectElement(self, col, row):
    print(col, row)
    print(self)
    self.field[col][row] = True
    print(self)

def __str__(self):
    out = '\n'
    for col in self.field:
        for value in col:
            out += str(value) + ' '
        out += '\n'
    return out

输出:

0 0

False False False
False False False
False False False


True False False
True False False
True False False

列表初始化为:

self.list = [[False] * size] * size

标签: pythonpython-3.xlist

解决方案


我修复了它:

self.field = [[False] * size for n in range(size)]

推荐阅读