相关疑难解决方法(0)

为什么+运算符不会在.append()的情况下更改列表?

我正在通过Udacity和Dave Evans介绍了关于列表属性的练习

list1 = [1,2,3,4]
list2 = [1,2,3,4]

list1=list1+[6]
print(list1)
list2.append(6)
print(list2)

list1 = [1,2,3,4]
list2 = [1,2,3,4]

def proc(mylist):
    mylist = mylist + [6]

def proc2(mylist):
    mylist.append(6)

# Can you explain the results given by the four print statements below? Remove
# the hashes # and run the code to check.

print (list1)
proc(list1)
print (list1)

print (list2)
proc2(list2)
print (list2)
Run Code Online (Sandbox Code Playgroud)

输出是

[1, 2, 3, 4, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]
[1, 2, 3, …
Run Code Online (Sandbox Code Playgroud)

python list concatenation append

7
推荐指数
1
解决办法
1650
查看次数

标签 统计

append ×1

concatenation ×1

list ×1

python ×1