【发布时间】:2021-09-06 01:06:20
【问题描述】:
所以基本上我想要一个数组(结果),我将第二个数组(数组)的总和添加到其中,但是 当我尝试添加到数组的结果总和时,它会添加到数组中应该添加到结果中的内容
def some_function(signature, n):
array = result = signature
count = 1
while count <= n:
print(array) # prints [1, 1, 1]
result.append(sum(array)) # Here i don't get it why result.append modifies array.
# Without result.append everything works fine
print(array) # prints [1, 1, 1, 3]
array.append(sum(array))
print(array) # prints [1, 1, 1, 3, 6]
del array[0]
count += 1
return result
print(some_function([1, 1, 1], 10))
【问题讨论】:
-
因为
array和results都指向同一个数组。signature也会随着他们而改变。
标签: python arrays sum append python-3.9