将List作为参数传入后预期该list可变,结果未变
def merge( nums1, m, nums2, n):
a=nums1[:m]
b=nums2[:n]
a.extend(b)
print(a)
nums1=a[:]
print(nums1)
nums1=[1,2,3,0,0,0]
nums2=[2,5,6]
m=3
n=3
merge(nums1,3,nums2,3)
print(nums1)
预期以上程序执行后会输出:
[1, 2, 3, 2, 5, 6]
[1, 2, 3, 2, 5, 6]
[1, 2, 3, 2, 5, 6]
实际上输出的是:
[1, 2, 3, 2, 5, 6]
[1, 2, 3, 2, 5, 6]
[1, 2, 3, 0, 0, 0]
nums1未变,原因未知,因此询问诸位。
You can find the result as above as end of function
merge
. New lists created fora
,b
andnums1
assigned by slice of another list.Do it like
then it will work !