AttributeError: 'tuple' object has no attribute 'extend'
出错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'的更多相关文章
- AttributeError: 'NoneType' object has no attribute 'extend'
Python使用中可能遇到的小问题 AttributeError: 'NoneType' object has no attribute 'extend' 或者AttributeError: 'Non ...
- Django报:AttributeError: tuple object has no attribute get
def index(request): hero_list=models.HeroInfo.objects.all() return render_to_response('index.html',{ ...
- AttributeError: 'list' object has no attribute 'extends' && list详解
拼写错误 是extend 而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...
- AttributeError: 'list' object has no attribute 'write_pdf'
我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...
- attributeError:'module' object has no attribute ** 解决办法
写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...
- AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'
/*************************************************************************** * AttributeError: 'modu ...
- AttributeError: 'dict_values' object has no attribute 'translate'
/***************************************************************************************** * Attribu ...
- python3 AttributeError: 'NoneType' object has no attribute 'split'
from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...
随机推荐
- YTU 2955: A改错题--销售部的打印机
2955: A改错题--销售部的打印机 时间限制: 1 Sec 内存限制: 128 MB 提交: 61 解决: 47 题目描述 销售部新进了一台快速打印机,使用频率很高.为了能够对打印情况进行统计 ...
- mac系统怎么给文件夹加密?mac文件夹加密教程
mac系统怎么给文件夹加密?目前来说,若想要对你的Mac OS下面的文件进行加密的话有三种方法可以可以做到,第一种方法,Mac自带磁盘工具:第二种方法,例如BatterZip此类Mac压缩解压工具打包 ...
- I.MX6 wpa_cli 使用
/*********************************************************************** * I.MX6 wpa_cli 使用 * 说明: * ...
- ThreadLocal工具类的使用(隔离思想)
ThreadLocal不是用来解决共享对象的多线程访问问题的, 通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象, ...
- bzoj1296 [SCOI2009]粉刷匠——背包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1296 对于不同木板之间,最终统计答案时做一个分组背包即可: 而要进行分组背包,就需要知道每个 ...
- charCode 表示空格 实现中文对齐
字符以及HTML实体 描述以及说明 这是我们使用最多的空格,也就是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个).要使用html实体表示才可累 ...
- 2-4 测试案例helloWorld
- SOLID总结(未完待续)
SOLID原则是著名的面向对像设计五原则,之所以要引入这些原则,是为了给以防止代码腐化而进行的重构活动定下切实可行的目标. 随着系统开发的进行,产品代码不可避免地会不断腐化,就算在开发过程中很好地应用 ...
- linux文件名乱码时删除或改名的方式(转载)
转自:http://www.linuxsa.cn/when-linux-file-name-topsy-turvy-deleted-or-renamed.html linux文件名乱码时删除或改名的方 ...
- Excel -- 实用技巧
一起来学习这45个职场Excel小窍门,轻轻松松提高工作效率,超实用,新技能get! 1.快速填充公式的三种方式 2.最快求和 3.添加文字下面线条的2种方法 4.设置列宽的3种方法 5.以cm为单位 ...