出错demo

 In [5]: a.extend([4,5])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-42b5da5e2956> in <module>
----> 1 a.extend([4,5]) AttributeError: 'tuple' object has no attribute 'extend'

打印一下tuple类型的属性可以看到,tuple类型除内置类型外,只有count和index两个属性

extend是list类型的方法

 In [3]: dir(tuple)
Out[3]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']

extend是1个列表+另一个列表,

它会改变原来列表的长度,而不会生成一个新的列表

所以,如果你使用了a=list.extend(XXX),它并不会如你希望 返回一个列表,而是一个None

示例 :

 In [6]: b=[1,2]                                                                 

 In [7]: c=b.extend([3])                                                         

 In [8]: b
Out[8]: [1, 2, 3] In [10]: print(c)
None In [11]: type(c)
Out[11]: NoneType

顺便说明一下:

count表示统计元组中指定的1个元素 出现的次数

 In [20]: b=(2,2,2,3,4)                                                          

 In [21]: b.count(2)
Out[21]: 3

index返回指定元素第1次出现的位置(下标)

 In [20]: b=(2,2,2,3,4)                                                          

 In [22]: b.index(3)
Out[22]: 3 In [23]: b.index(2)
Out[23]: 0

附:

list的所有属性如下

 In [4]: dir(list)
Out[4]:
['__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']

AttributeError: 'tuple' object has no attribute 'extend'的更多相关文章

  1. AttributeError: 'NoneType' object has no attribute 'extend'

    Python使用中可能遇到的小问题 AttributeError: 'NoneType' object has no attribute 'extend' 或者AttributeError: 'Non ...

  2. Django报:AttributeError: tuple object has no attribute get

    def index(request): hero_list=models.HeroInfo.objects.all() return render_to_response('index.html',{ ...

  3. AttributeError: 'list' object has no attribute 'extends' && list详解

    拼写错误 是extend  而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Persisting Data to the Device

    Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...

  2. Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)——无非是在传统遍历过程中修改叶子结点加入后继结点信息(传统是stack记录),然后再删除恢复

    先看看线索二叉树 n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域.利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索 ...

  3. Ruby: Call the system and get system information.

    1. Kill the task cmd2="taskkill /F /IM typeperf.exe"stdout2=%x{#{cmd2}} 2. Start counters: ...

  4. bzoj 3232 圈地游戏 —— 01分数规划+最小割建图(最大权闭合子图)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...

  5. .NET MVC API返回JSON对象

    方法多种,自己目前采用的是自定义返回格式的方法,不需要修改配置文件. 辅助类: public class ApiResponseHelper { public static HttpResponseM ...

  6. 【196】Dell 移动工作站系统安装方法

    会出现找不到硬盘的情况,解决方法:安装系统的时候需要加载阵列卡驱动 下载阵列卡驱动,以 Dell T7610 为例根据安装系统进行选择,地址:http://zh.community.dell.com/ ...

  7. #if、#ifdef、#if defined之间的区别(转载)

    转自:http://www.yucoat.com/c_program/difference_if_ifdef_if_defined.html #if的使用说明 #if的后面接的是表达式 #if (MA ...

  8. bzoj 4236: JOIOJI【前缀和+map】

    设sj,so,si分别是J O I的个数前缀和,然后要求求最长(l,r)满足sj[r]-sj[l-1]==so[r]-so[l-1]==si[r]-si[l-1],化简一下就是满足so[r]-so[l ...

  9. bzoj 1502 月下柠檬树【Simpson积分】

    投影到地面之后,会发现圆形在平行光下面积和形状是不会变的,也就是所要求的图形是若干个圆和把相邻两个圆连起来的公切线所组成的. 公切线和圆间距瞎求一下就行,注意要去掉被完全覆盖的圆 然后simpson即 ...

  10. bzoj 4818: [Sdoi2017]序列计数【容斥原理+dp+矩阵乘法】

    被空间卡的好惨啊---- 参考:http://blog.csdn.net/coldef/article/details/70305596 容斥,\( ans=ans_{没有限制}-ans{没有质数} ...