拼写错误

是extend  而不是extends

出错demo:

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

 In [28]: c.extends([5])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-28-2022e87158c8> in <module>
----> 1 c.extends([5]) AttributeError: 'list' object has no attribute 'extends'

调试:

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

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

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

 In [29]: dir(list)
Out[29]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

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

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

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

In [34]: c.append(4)                                                            

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

list.clear()清空整个列表

In [35]: c
Out[35]: [2, 3, 4] In [36]: c.clear() In [37]: c
Out[37]: []

list.copy()返回一个复制的列表

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

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

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

list.count(obj)统计指定元素在列表中出现的次数

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

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

 In [49]: c.extend([5])                                                          

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

错误示例:

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

list.index(obj)返回指定元素第1次出现在列表中的位置

 In [53]: c
Out[53]: [2, 3, 2, 5, 'a'] In [54]: In [54]: c.index("a")
Out[54]: 4

list.insert(index,obj) 将指定对象插入到列表中指定的位置

注意 :通过insert方法 ,将obj插入到index的位置后,原来在index位置的元素并不会被覆盖,而是会整体往后移。

 In [56]: c
Out[56]: [2, 3, 2, 5, 'b'] In [58]: c.insert(4,"a") In [59]: c
Out[59]: [2, 3, 2, 5, 'a', 'b']

这一点c[3]="c"这种写法是不同的

像上面这样直接通过 = 给元素附值,会把列表中原来的值覆盖掉

In [61]: c
Out[61]: [2, 3, 2, 5, 'a', 'b'] In [62]: c.index(5)
Out[62]: 3 In [63]: c[3]="c" In [64]: c
Out[64]: [2, 3, 2, 'c', 'a', 'b']

如果指定index超过元素的长度,也不会报错,而是直接将对象插入到列表的末尾,相当于执行了append

 In [64]: c
Out[64]: [2, 3, 2, 'c', 'a', 'b'] In [65]: len(c)
Out[65]: 6 In [66]: c.insert(7,"aaa") In [67]: c
Out[67]: [2, 3, 2, 'c', 'a', 'b', 'aaa'] In [68]: c.index("aaa")
Out[68]: 6

list.pop(index)从列表中删除指定元素,返回删除的元素,不指定index则会直接删除最后1个元素

 In [69]: c
Out[69]: [2, 3, 2, 'c', 'a', 'b', 'aaa'] #删除下标为0的元素
In [70]: d = c.pop(0) In [71]: d
Out[71]: 2 In [72]: c
Out[72]: [3, 2, 'c', 'a', 'b', 'aaa'] #不指定下标,默认删除最后1个元素
In [73]: e= c.pop() In [74]: e
Out[74]: 'aaa'

list.remove(obj)移除列表中 指定的下标元素,无返回值

 In [79]: c
Out[79]: [3, 2, 'c', 'a', 'b'] In [80]: c.remove(3) In [82]: c
Out[82]: [2, 'c', 'a', 'b']

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

 In [89]: c
Out[89]: ['c', 'a', 'b'] In [90]: c.sort() In [91]: c
Out[91]: ['a', 'b', 'c']

注意 :使用list.sort()方法,要求list中的元素都是同1数据类型,否则会报 这样的错误 :TypeError: '<' not supported between instances of 'xxx' and 'xxx'

In [85]: c
Out[85]: [2, 'c', 'a', 'b'] In [86]: c.sort()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-86-3b6e9baacecd> in <module>
----> 1 c.sort() TypeError: '<' not supported between instances of 'str' and 'int'

AttributeError: 'list' object has no attribute 'extends' && list详解的更多相关文章

  1. AttributeError: 'unicode' object has no attribute 'tzinfo' 未解决

    Internal Server Error: /demo/machineinfo.htmlTraceback (most recent call last): File "C:\Python ...

  2. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  3. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

  4. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  5. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  6. AttributeError: 'dict_values' object has no attribute 'translate'

    /***************************************************************************************** * Attribu ...

  7. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

  8. 对于AttributeError: 'Flask' object has no attribute 'cli'的解决办法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 环境flask-script2.0.5.flask0.10.1 运行官方文档sample 出现问题 c:\kk\flask\examples\fl ...

  9. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

随机推荐

  1. 揭开自然拼读法(Phonics)的神秘面纱

    揭开自然拼读法(Phonics)的神秘面纱 自然拼读法  (Phonics),是指看到一个单词,就可以根据英文字母在单词里的发音规律把这个单词读出来的一种方法.即从“字母发音-字母组合发音-单词-简单 ...

  2. frameset 框架整体退出登录的问题

    1 设置其他的页面都验证session,如果session不存在就跳转到 Login 页: 2 Login中添加下面的js代码: <script language="JavaScrip ...

  3. bzoj 3991 寻宝游戏

    题目大意: 一颗树 有一个点的集合 对于每个集合的答案为 从集合内一个点遍历集合内所有点再返回的距离最小值 每次可以选择一个点 若在集合外便加入集合 若在集合内就删除 求每次操作后这个集合的答案 思路 ...

  4. BZOJ2283: [Sdoi2011]火星移民

    Description 在2xyz年,人类已经移民到了火星上.由于工业的需要,人们开始在火星上采矿.火星的矿区是一个边长为N的正六边形,为了方便规划,整个矿区被分为6*N*N个正三角形的区域(如图1) ...

  5. AutoIT: 通过页面抓取来陈列任务管理器里面所有进程的列表

    #include<Array.au3> $handle =WinGetHandle("Windows 任务管理器") ;$ctrl =ControlGetHandle( ...

  6. gcc编译系统

    一. C语言编译过程 C语言的编译过程可分为四个阶段: 1.预处理(Preprocessing) 对源程序中的伪指令(即以#开头的指令)和特殊符号进行处理的过程. 伪指令包括:1)宏定义指令: 2)条 ...

  7. TransposonPSI——转座子分析的入门自学

    最近需要做转座子分析,查找发现可以使用 TransposonPSI 来进行分析.但是登陆官网,该软件 update 时间为 2013 年,但是因为时间紧迫,暂时还没有进行其他方法的调研,所以先选用该软 ...

  8. bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】

    如果确定了第一行,那么可以推出来整个矩阵,矩阵合法的条件是n+1行全是0 所以推出来n+1行和1行的关系,然后用异或高斯消元来解即可 #include<iostream> #include ...

  9. Survival on the Titanic (泰坦尼克号生存预测)

    >> Score 最近用随机森林玩了 Kaggle 的泰坦尼克号项目,顺便记录一下. Kaggle - Titanic: Machine Learning from Disaster On ...

  10. JS 自写datapage.js 通用分页

    var Page = function () { }; Page.prototype = {     Loading: "<img src='/Content/Scripts/Data ...