Python只在线路板的一行中写入值

Python只在线路板的一行中写入值,python,Python,因此,我尝试创建一个程序,该程序使用在列表中创建列表的板: [[0,0,0,…,L],[0,0,0,…,L],[0,0,0,…,L],[0,0,0,…,L],…,H]] x=4和y=2(x=H,y=L)的示例: >> [[0,0,0,0],[0,0,0,0]] 我的代码是: ... #>> Initialistation board = [] temp = [] H = int(input('height : ')) L = int(input('lenght : ')) fo

因此,我尝试创建一个程序,该程序使用在列表中创建列表的板:

[[0,0,0,…,L],[0,0,0,…,L],[0,0,0,…,L],[0,0,0,…,L],…,H]]
x=4和y=2(x=H,y=L)的示例:
>> [[0,0,0,0],[0,0,0,0]]
我的代码是:

...
#>> Initialistation
board = []
temp = []

H = int(input('height : '))
L = int(input('lenght : '))

for i in range(0,L):
    temp.append('0')

for j in range(0,H):
    board.append(temp)

temp_x = int(input('x :'))
temp_y = int(input('y :'))
board[temp_y[temp_x]] = 'f' # line 29

print(board)
基本上,我会在我的董事会中增加价值,但我尝试过的任何事情都不会像我那样工作:

我的结果是:

# with the example above
board[0][1] = 'f'
>> [[0,0,'f',0],[0,0,'f',0]
预期结果:

# with the example above
board[0][1] = 'f'
>> [[0,0,0,0],[0,0,'f',0]
我所尝试的:

board[[0][1]] = 'f'
>> IndexError (line 29): list index out of range
问题是“f”写入电路板的每一行,而不仅仅是预期的一行
那么我如何才能得到预期的结果呢?

您想要的应该是:

a  = [[0,0,0,0],[0,0,0,0]]

a[1][2] = 'f'

print(a)
输出:

[[0, 0, 0, 0], [0, 0, 'f', 0]]
编辑

现在问题很清楚了。 你之所以会有这种行为,是因为你是如何创建董事会的。 电路板中的每一行都引用相同的列表(临时)。 因此,如果您认为您更改了一行,那么您将更改所有行,因为您更改了所有行引用的值

您可以将板的创建更改为一行(列表):

或嵌套循环:

board = []
for j in range(0,H):
    temp = []
    for i in range(0,L):
        temp.append('0')

    board.append(temp)
包含更改的完整代码:

H = int(input('height : '))
L = int(input('lenght : '))

board = [['0' for i in range(L)] for k in range(H)]

temp_x = int(input('x :'))
temp_y = int(input('y :'))

print(temp_x)
print(temp_y)

board[temp_y][temp_x] = 'f' # line 29

print(board)
生成输出:

height : 2
lenght : 2
x :1
y :1
1
1
[['0', '0'], ['0', 'f']]

你想要的应该是:

a  = [[0,0,0,0],[0,0,0,0]]

a[1][2] = 'f'

print(a)
输出:

[[0, 0, 0, 0], [0, 0, 'f', 0]]
编辑

现在问题很清楚了。 你之所以会有这种行为,是因为你是如何创建董事会的。 电路板中的每一行都引用相同的列表(临时)。 因此,如果您认为您更改了一行,那么您将更改所有行,因为您更改了所有行引用的值

您可以将板的创建更改为一行(列表):

或嵌套循环:

board = []
for j in range(0,H):
    temp = []
    for i in range(0,L):
        temp.append('0')

    board.append(temp)
包含更改的完整代码:

H = int(input('height : '))
L = int(input('lenght : '))

board = [['0' for i in range(L)] for k in range(H)]

temp_x = int(input('x :'))
temp_y = int(input('y :'))

print(temp_x)
print(temp_y)

board[temp_y][temp_x] = 'f' # line 29

print(board)
生成输出:

height : 2
lenght : 2
x :1
y :1
1
1
[['0', '0'], ['0', 'f']]

我终于找到了awnser:

问题出现在board.append(temp)行,因为它将literally temp追加为“列表temp”,而不是“[0,0,0,0]”

解决这一问题的方法是用“*”将temp解组,然后用“[]”将其重新组合为一个列表 所以这句话:
board.append(临时)
应该是:
board.append([*temp])
我终于找到了awnser:

问题出现在board.append(temp)行,因为它将literally temp追加为“列表temp”,而不是“[0,0,0,0]”

解决这一问题的方法是用“*”将temp解组,然后用“[]”将其重新组合为一个列表 所以这句话:
board.append(临时)
应该是:
board.append([*temp])

试着写下你拥有的(你之前的列表)和你期望的输出(在想要的代码行后面的列表)我之前的列表是
[[0,0,0,0],[0,0,0,0,0,0,0]]]
然后我得到了
[[0,0,'f',0],[0,0,'f',0]
,我期望的是“[[0,0,0,0,0,0]@Edhyjox,我想Florian的答案会对你有用的,只要改变
a[0][2]='f'
试着写下你拥有的(你之前的列表)和你期望的输出(在通缉的代码行之后的列表)我之前的列表是
[[0,0,0,0,0],[0,0,0]
然后我得到了
[[0,0,'f',0],[0,0,'f',0],[0,0]
,我期望的是[[0,0,0,0]@Edhyjox,我认为Florian的答案对你有用,只需更改
a[0][2]='f'
预期输出是什么?预期输出可能重复?预期输出可能重复