【发布时间】:2012-04-13 23:28:08
【问题描述】:
>>> aList = []
>>> aList += 'chicken'
>>> aList
['c', 'h', 'i', 'c', 'k', 'e', 'n']
>>> aList = aList + 'hello'
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
aList = aList + 'hello'
TypeError: can only concatenate list (not "str") to list
我不明白为什么 list += (something) 和 list = list + (something) 会做不同的事情。另外,为什么+= 将字符串拆分为要插入到列表中的字符?
【问题讨论】:
-
另一个类似的问题stackoverflow.com/q/9766387/776084.
-
@agf:不,这个问题是关于
+=与+面对对同一个列表的多次引用。 -
在我看来不太像复制品。