拼写错误

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

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

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

  1. In [33]: c = [2,3]
  2.  
  3. In [34]: c.append(4)
  4.  
  5. In [35]: c
  6. Out[35]: [2, 3, 4]

list.clear()清空整个列表

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

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

  1. In [38]: c = [2,3]
  2.  
  3. In [39]: b = c.copy()
  4.  
  5. In [40]: b
  6. Out[40]: [2, 3]

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

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

list.extend(obj) 将obj追加到list的末尾.注意 obb应该是可迭代的对象,否则会报 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

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

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

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

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

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

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

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

  1. In [61]: c
  2. Out[61]: [2, 3, 2, 5, 'a', 'b']
  3.  
  4. In [62]: c.index(5)
  5. Out[62]: 3
  6.  
  7. In [63]: c[3]="c"
  8.  
  9. In [64]: c
  10. Out[64]: [2, 3, 2, 'c', 'a', 'b']

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

  1. In [64]: c
  2. Out[64]: [2, 3, 2, 'c', 'a', 'b']
  3.  
  4. In [65]: len(c)
  5. Out[65]: 6
  6.  
  7. In [66]: c.insert(7,"aaa")
  8.  
  9. In [67]: c
  10. Out[67]: [2, 3, 2, 'c', 'a', 'b', 'aaa']
  11.  
  12. In [68]: c.index("aaa")
  13. Out[68]: 6

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

  1. In [69]: c
  2. Out[69]: [2, 3, 2, 'c', 'a', 'b', 'aaa']
  3.  
  4. #删除下标为0的元素
  5. In [70]: d = c.pop(0)
  6.  
  7. In [71]: d
  8. Out[71]: 2
  9.  
  10. In [72]: c
  11. Out[72]: [3, 2, 'c', 'a', 'b', 'aaa']
  12.  
  13. #不指定下标,默认删除最后1个元素
  14. In [73]: e= c.pop()
  15.  
  16. In [74]: e
  17. Out[74]: 'aaa'

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

  1. In [79]: c
  2. Out[79]: [3, 2, 'c', 'a', 'b']
  3.  
  4. In [80]: c.remove(3)
  5.  
  6. In [82]: c
  7. Out[82]: [2, 'c', 'a', 'b']

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

  1. In [89]: c
  2. Out[89]: ['c', 'a', 'b']
  3.  
  4. In [90]: c.sort()
  5.  
  6. In [91]: c
  7. Out[91]: ['a', 'b', 'c']

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

  1. In [85]: c
  2. Out[85]: [2, 'c', 'a', 'b']
  3.  
  4. In [86]: c.sort()
  5. ---------------------------------------------------------------------------
  6. TypeError Traceback (most recent call last)
  7. <ipython-input-86-3b6e9baacecd> in <module>
  8. ----> 1 c.sort()
  9.  
  10. 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. YTU 2800: 逗逗泡泡的保密电文

    2800: 逗逗泡泡的保密电文 时间限制: 1 Sec  内存限制: 128 MB 提交: 229  解决: 114 题目描述 某电报局的电文保密的规律是将每个英文字母变成其后的第4个字母(很原始的那 ...

  2. eclipse中jsp页面乱码问题

    若上述位置均改为utf-8之后,页面展示扔为乱码,检查jsp页面是否有编码说明

  3. myeclipse 8.6安装SVN插件

    方法二: 安装subclipse, SVN 插件   1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigris.org,    2.从中解压出features与plug ...

  4. bzoj-1192 [HNOI2006]鬼谷子的钱袋(水题)

    题目链接: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec  Memory Limit: 162 MB Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各 ...

  5. [转]python_常用断言assert

    原文地址:http://www.jianshu.com/p/eea0b0e432da python自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行 ...

  6. poj 2411((多米诺骨牌问题))

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12854   Accepted: 748 ...

  7. ObjectInputStream与ObjectOutputStream类实现对象的存取

    1. ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入 ...

  8. oracle 备份数据

    exp AC_SSO/AC_SSO@HB file=d:\wamp\Golden3C_AuthenticationCenter.dmp owner=AC_SSO full=y用户名/密码@服务 exp ...

  9. Eclipse如何打开Android工程(转载)

    转自:http://www.cnblogs.com/kernel-style/p/3339102.html 一.Eclipse如何打开Android工程 1.你可以在file->new-> ...

  10. 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】

    用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...