函数 | 解释 |
---|---|
s.append(x) |
在序列s的尾部追加x(等同于s[len(s):len(s)] = [x] ) |
s.extend(t) or s += t
|
把t的数据,扩展到序列s(大致等同于s[len(s):len(s)] = t ) |
表中,s是一个可变序列类型(如List
)的实例,t是任何可迭代对象,x是一个任意对象,且其类型和值的范围由s的限制(例如,bytearray
只接收整数且值范围0 <= x <= 255
)。
>>> s = [1, 2, 3]
>>> x = 4
>>> t = [5, 6]
>>>s.append(x)
>>> s
[1, 2, 3, 4]
>>> s.append(t)
>>> s
[1, 2, 3, 4, [5, 6]]
>>> s.extend(x)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> s.extend(t)
>>> s
[1, 2, 3, 4, [5, 6], 5, 6]