使用python中的函数更新列表中的所有项
你能告诉我我做错了什么以及如何修复它吗 谢谢 我有一个功能使用python中的函数更新列表中的所有项,python,list,function,Python,List,Function,你能告诉我我做错了什么以及如何修复它吗 谢谢 我有一个功能 def out(some_list): test_list = [1,2,3,4] result = [] for i in some_list: if i == 1: test_list = [0,0,0,0] else: test_list = test_list result.append(test_list
def out(some_list):
test_list = [1,2,3,4]
result = []
for i in some_list:
if i == 1:
test_list = [0,0,0,0]
else:
test_list = test_list
result.append(test_list)
return result
如果我们打印出来,它将返回:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
我需要回去
[[0, 0, 0, 0], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
这是因为您设置了
test\u list=[0,0,0,0]
因此,即使在test\u list=test\u list
中,它也会将结果设置为[0,0,0,0]
试用
def out(some_list):
test_list = [1,2,3,4]
result = []
for i in some_list:
if i == 1:
result.append([0,0,0,0])
else:
result.append(test_list)
return result
这是因为您在此函数中传递的列表将
1
作为第一个元素的值。例如:
out([1,2,3,4]) # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
逐步检查您的代码:
test_list = [1,2,3,4]
result = []
for i in some_list: # The value of each element in some_list
if i == 1: # If the value is "1" set test_list: [0,0,0,0]
test_list = [0,0,0,0]
else:
test_list = test_list # Otherwise set test_list to itself (doing nothing)
result.append(test_list)
i
的for循环值是您在some_list
中所在元素的值,而不是我们所在元素在列表中的索引或位置(如本问题所示)
如果该值为1
,则测试列表将设置为[0,0,0,0]
。一旦命中,只有值[0,0,0,0]
将附加到结果
。因此,如果第一个元素是1
,那么您只能在结果中看到值[0,0,0,0]
,否则您将看到[1,2,3,4]
,直到循环到达列表中的值是1
的位置
以下是一些例子:
out([0,1,2,3]) # [[1, 2, 3, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([1,2,3,4]) # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([2,2,5,1]) # [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]]
希望这能让你更清楚为什么你会得到这个结果
编辑
就您更新的问题而言,这里发生的是,当您调用.append(fig)
时,它只是内存中对fig
的引用的副本。基本上,每当它更改时,您附加的所有副本也会更改。有两种方法可以处理此问题,第一种方法是在循环的范围内定义变量fig
,这样每个循环上都是一个新的、不同的变量:
for i in test_list:
fig = [2, 1] # <== In the scope of the loop, so each fig is it's on variable
...
test\u list=test\u list
是什么意思?你为什么这么做?它不会向代码中添加任何内容。当我运行此代码时,它不会返回[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]
。我得到了[[1,2,3,4],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
。请确保您的问题是正确的。some\u list
的值是多少?如果第一个元素是值1
,那么它将是[[0,0,0,0],[0,0,0,0,0],[0,0,0,0],[0,0,0,0]
。您没有检查您是否在第一个索引上,而是检查some\u list
中的值是否为1
。您可以演示如何调用out
函数吗?没有它,你的例子就不完整。此外,还将简要说明函数背后的逻辑。如何将输入转换为输出?1与任何事情有什么关系?请不要以使现有答案无效的方式编辑问题。嗨,斯宾塞,谢谢你的解释,但我需要根据具体情况重新编写测试列表conditions@Dmitriy_kzn你能通过在问题中添加更多信息来澄清你的意思吗?@Dmitriy_kzn我已经更新了我的问题,说明了为什么会发生这种情况。基本上,它保留了对图的引用,因此,任何时候它都会更改之前的所有附加项(作为引用)也会更改。上一个示例说明了在while循环的作用域中实际创建一个新变量test\u list
,与函数作用域中的变量不同。谢谢Spencer,现在已经清楚了。再次非常感谢
out([0,1,2,3]) # [[1, 2, 3, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([1,2,3,4]) # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([2,2,5,1]) # [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]]
for i in test_list:
fig = [2, 1] # <== In the scope of the loop, so each fig is it's on variable
...
for i in test_list:
if i == '0':
fig[0] = off
fig[1] = off
elif i == '1':
fig[0] = off
fig[1] = on
new_list.append(fig[:]) # <== Copy the array fig and append that value