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 ...
随机推荐
- ubuntu mysql5.7源码安装
本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan ...
- YTU 2754: C++习题-快速排序
2754: C++习题-快速排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 92 解决: 55 题目描述 以某个数为标准,把比这个数大的都移到它的后面,比这个数小的都移到它的前面, ...
- Bootstrap中的各种下拉菜单
@*基本下拉菜单与按钮下拉菜单的样式完全一致.不过,基本的下拉菜单使用<div class="dropdown">包裹,所有要换行.而按钮式下拉菜单<div cl ...
- hdu4815 概率问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 好久没写dp了..最开始题意都理解错了, 哎!!我现在很饿也很困!! AC代码: #includ ...
- 洛谷 P1081 开车旅行【双向链表+倍增】
倍增数组的20和N写反了反复WAWAWA-- 注意到a和b在每个点上出发都会到一个指定的点,所以这样构成了两棵以n点为根的树 假设我们建出了这两棵树,对于第一问就可以枚举起点然后倍增的找出ab路径长度 ...
- bzoj 2839: 集合计数【容斥原理+组合数学】
首先,考虑容斥,我们所要的答案是并集至少有\( k \)个数的方案数减去并集至少有\( k+1 \)个数的方案数加上并集至少有\( k \)个数的方案数-- 在n个数中选i个的方案数是\( C_{n} ...
- Docker的基础知识与安装(Ubantu CentOS)
Docker是一种允许特殊类型虚拟化的实用程序.Docker允许使用Docker镜像在系统上“虚拟”运行程序.可以下载或创建可以运行的图像.一旦加载并执行了图像,它现在就是一个容器.容器已为容器内的程 ...
- UTF-8格式的文本文件程序读取异常
最近在windows服务器上直接创建并手打输入配置参数,比如设置概率0.6,然后用java程序打开读取该参数,在本地linux环境下测试完全正常,但是一放到服务器上,就报NotNumber错误,查看了 ...
- mysql远程备份
相关链接:https://blog.csdn.net/LiuHuan_study/article/details/81512831https://www.cnblogs.com/ryanzheng/p ...
- [POI2007]天然气管道Gaz
Description Mary试图控制成都的天然气市场.专家已经标示出了最好的天然气井和中转站在成都的地图.现在需要将中转站和天然气井连接起来.每个中转站必须被连接到正好一个钻油井,反之亦然. Ma ...