AttributeError: 'tuple' object has no attribute 'extend'

 

Error demo

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])
6 
7 AttributeError: 'tuple' object has no attribute 'extend'

 

Print it tuple type of property you can see, tuple type in addition to built-in types, and index only count two properties

extend list type is a method

 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 a list is another list +,

It will change the length of the original list, but will not generate a new list

So, if you use a = list.extend (XXX), it will not if you want to return a list, but a None

Example:

 1 In [6]: b=[1,2]                                                                 
 2 
 3 In [7]: c=b.extend([3])                                                         
 4 
 5 In [8]: b                                                                       
 6 Out[8]: [1, 2, 3] 
 9 
10 In [10]: print(c)                                                               
11 None
12 
13 In [11]: type(c)                                                                
14 Out[11]: NoneType

 

By the way:

statistics count represents the number of tuples in a specified elements appear

1 In [20]: b=(2,2,2,3,4)                                                          
2 
3 In [21]: b.count(2)                                                             
4 Out[21]: 3

Returns the index of the position of the specified element occurs first (subscript)

1 In [20]: b=(2,2,2,3,4)                                                          
2 
3 In [22]: b.index(3)                                                             
4 Out[22]: 3
5 
6 In [23]: b.index(2)                                                             
7 Out[23]: 0

 

 Attachment:

List all the properties as follows

 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']

 

 

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11077232.html