相关疑难解决方法(0)

Python列表+ = iterable的行为是否记录在任何地方?

看起来在Python中,list += x适用于任何可迭代的x:

In [6]: l = []

In [7]: l += [1]

In [8]: l += (2, 3)

In [9]: l += xrange(5)

In [10]: l
Out[10]: [1, 2, 3, 0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

这种行为记录在哪里吗?

为了与之形成对比list + x,后者只有在x同样的情况下才有效list.这在文档中有详细说明.

python containers operators

28
推荐指数
2
解决办法
786
查看次数

为什么+ =运算符的工作方式与+不同,并为python字典赋值运算符?

有人可以解释一下这种行为:

>>> a = {'hello':'world' , 'good':'food'}
>>> b = [1,2]
>>> b = b + a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "dict") to list
>>> b += a
>>> b
[1, 2, 'good', 'hello'] <--- Why do the keys get added when dict cannot be added to a list
>>> 
Run Code Online (Sandbox Code Playgroud)

python

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

标签 统计

python ×2

containers ×1

operators ×1