所以,这是我编写的代码:
def remove(i, xs):
xs = xs[:i] + xs[i+1:]
lst = [0, 1, 2, 3]
remove(1, lst)
print(lst)
输出为[0, 1, 2, 3]
,尽管我期望[0, 2, 3]
。我在做错什么我错过了吗?
奇怪的是,此代码有效,我也不明白:
def remove(i, xs):
xs[:] = xs[:i] + xs[i+1:]
lst = [0, 1, 2, 3]
remove(1, lst)
print(lst)
输出为[0, 2, 3]
为什么这样工作?