AttributeError: 'list' object has no attribute 'extends'

拼写错误

是extend  而不是extends

出错demo:

1 In [27]: c = [2,3]                                                              
2 
3 In [28]: c.extends([5])                                                         
4 ---------------------------------------------------------------------------
5 AttributeError                            Traceback (most recent call last)
6 <ipython-input-28-2022e87158c8> in <module>
7 ----> 1 c.extends([5])
8 
9 AttributeError: 'list' object has no attribute 'extends'

调试:

既然错误提示说list对象没有extends这个属性,那我们可以先来看一下list的属性都有什么

通过第42行,就可以看到list有extend属性,而不是extends属性

这样就知道代码中的错误是 拼写错误

 1 In [29]: dir(list)                                                              
 2 Out[29]: 
 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']

其它几个属性也演示一下吧

append 追加,是大家都很熟悉的list的属性。注意: 一次只能追加1个对象

In [33]: c = [2,3]                                                              

In [34]: c.append(4)                                                            

In [35]: c                                                                      
Out[35]: [2, 3, 4]

clear清空整个列表

扫描二维码关注公众号,回复: 6597144 查看本文章
In [35]: c                                                                      
Out[35]: [2, 3, 4]

In [36]: c.clear()                                                              

In [37]: c                                                                      
Out[37]: []

copy返回一个复制的列表

In [38]: c = [2,3]                                                              

In [39]: b = c.copy()                                                           

In [40]: b                                                                      
Out[40]: [2, 3]

count统计指定元素在列表中出现的次数

In [45]: c                                                                      
Out[45]: [2, 3, 2]

In [46]: c.count(2)                                                             
Out[46]: 2

a.extend(b) 将b追加到a的末尾.注意 b应该是可迭代的对象,否则会报 TypeError: 'xxx' object is not iterable错误

 1 In [49]: c.extend([5])                                                          
 2 
 3 In [50]: c                                                                      
 4 Out[50]: [2, 3, 2, 5]
 5 
 6 # 如果追加的是1个字典,会把字典的key追加到字典的末尾
 7 In [51]: a={"a":6}                                                              
 8 
 9 In [52]: c.extend(a)                                                            
10 
11 In [53]: c                                                                      
12 Out[53]: [2, 3, 2, 5, 'a']

错误示例:

 1 In [47]: c                                                                      
 2 Out[47]: [2, 3, 2]
 3 
 4 In [48]: c.extend(5)                                                            
 5 ---------------------------------------------------------------------------
 6 TypeError                                 Traceback (most recent call last)
 7 <ipython-input-48-5a85afdd21bf> in <module>
 8 ----> 1 c.extend(5)
 9 
10 TypeError: 'int' object is not iterable

index返回指定元素第1次出现在列表中的位置

insert将元素插入到列表中指定的位置

pop从列表中删除指定元素,返回删除的元素

remove移除列表中 指定的元素,无返回值

sort将列表中的内容进行排序,无返回值

猜你喜欢

转载自www.cnblogs.com/kaerxifa/p/11077404.html