Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
l = ["1","2"] e = "3" def add(e,s): s = s + [e] return s add(e,l) print(l)
为什么不打印['1','2','3']?
['1','2','3']
s + [e]是一个新列表。您将其分配给s函数中的变量add。
s + [e]
s
add
此变量与l外部代码中的变量不同。它一开始指的是同一个对象,但是当您将不同的对象分配给s.
l
可能您想要做的是更改引用的对象的内容s,而不是s引用不同的对象:
s.append(e)
这意味着sandl将继续引用同一个对象,并且该对象被修改。