1 In [5]: a.extend([4,5])
2 ---------------------------------------------------------------------------
3 AttributeError Traceback (most recent call last)
4 <ipython-input-5-42b5da5e2956> in <module>
5 ----> 1 a.extend([4,5])
7 AttributeError: 'tuple' object has no attribute 'extend'
打印一下tuple类型的属性可以看到,tuple类型除内置类型外,只有count和index两个属性
extend是list类型的方法
1 In [3]: dir(tuple)
2 Out[3]:
3 ['__add__',
4 '__class__',
5 '__contains__',
6 '__delattr__',
7 '__dir__',
8 '__doc__',
9 '__eq__',
10 '__format__',
11 '__ge__',
12 '__getattribute__',
13 '__getitem__',
14 '__getnewargs__',
15 '__gt__',
16 '__hash__',
17 '__init__',
18 '__init_subclass__',
19 '__iter__',
20 '__le__',
21 '__len__',
22 '__lt__',
23 '__mul__',
24 '__ne__',
25 '__new__',
26 '__reduce__',
27 '__reduce_ex__',
28 '__repr__',
29 '__rmul__',
30 '__setattr__',
31 '__sizeof__',
32 '__str__',
33 '__subclasshook__',
34 'count',
35 'index']
extend是1个列表+另一个列表,
它会改变原来列表的长度,而不会生成一个新的列表
所以,如果你使用了a=list.extend(XXX),它并不会如你希望 返回一个列表,而是一个None
1 In [6]: b=[1,2]
3 In [7]: c=b.extend([3])
5 In [8]: b
6 Out[8]: [1, 2, 3]
10 In [10]: print(c)
11 None
13 In [11]: type(c)
14 Out[11]: NoneType
顺便说明一下:
count表示统计元组中指定的1个元素 出现的次数
1 In [20]: b=(2,2,2,3,4)
3 In [21]: b.count(2)
4 Out[21]: 3
index返回指定元素第1次出现的位置(下标)
1 In [20]: b=(2,2,2,3,4)
3 In [22]: b.index(3)
4 Out[22]: 3
6 In [23]: b.index(2)
7 Out[23]: 0
list的所有属性如下
1 In [4]: dir(list)
2 Out[4]:
3 ['__add__',
4 '__class__',
5 '__contains__',
6 '__delattr__',
7 '__delitem__',
8 '__dir__',
9 '__doc__',
10 '__eq__',
11 '__format__',
12 '__ge__',
13 '__getattribute__',
14 '__getitem__',
15 '__gt__',
16 '__hash__',
17 '__iadd__',
18 '__imul__',
19 '__init__',
20 '__init_subclass__',
21 '__iter__',
22 '__le__',
23 '__len__',
24 '__lt__',
25 '__mul__',
26 '__ne__',
27 '__new__',
28 '__reduce__',
29 '__reduce_ex__',
30 '__repr__',
31 '__reversed__',
32 '__rmul__',
33 '__setattr__',
34 '__setitem__',
35 '__sizeof__',
36 '__str__',
37 '__subclasshook__',
38 'append',
39 'clear',
40 'copy',
41 'count',
42 'extend',
43 'index',
44 'insert',
45 'pop',
46 'remove',
47 'reverse',
48 'sort']