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 ...
随机推荐
- UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...
- bzoj3195 [Jxoi2012]奇怪的道路——状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3195 看到数据范围就应该想到状压呢... 题解(原来是这样):https://www.cnb ...
- bzoj 2809: [Apio2012]dispatching【dfs序+主席树】
可并堆就可以,但是想复健一下主席树. 考虑枚举管理者,然后选忍者的时候在子树中贪心的从小到大选.做成dfs序就是选区间内和小于等于k的最多点.可以用主席树,查询的时候在主席树上二分即可 这里注意,为了 ...
- SS上网配置(Window 7/8/10 )详解
SS很多人都会用到,尤其是做外贸的朋友,今天我们来说下SS相关的配置. 首先从官网下载解压后的目录如结构下: 点击***.exe,选择以管理员身份运行,切记打开后界面如下 服务器地址为一段I ...
- .NET CORE技术路线图
分享一张图.来自微信.不做解释.
- [POI2008]BLO-Blockade 【无向图tarjan/鸽点】By cellur925
题目传送门 lyd无向图tarjan的例题. 一句话题意(不得不佩服lyd老师的高度概括能力):在一张无向连通图上,求出每个点被破坏(去掉与这个点相关的所有边,不去掉这个点)后,无向图中使i,j不连通 ...
- (转载)Python一篇学会多线程
Python 一篇学会多线程 链接:https://www.cnblogs.com/yeayee/p/4952022.html 多线程和多进程是什么自行google补脑,廖雪峰官网也有,但是不够简洁 ...
- java String类为什么是final的
1.为了安全 java 必须借助操作系统本身的力量才能做事,jdk提供的很多核心类比如String,这类内的很多方法 都不是java编程语言本身编写的,很多方法都是调用操作系统本地的api,如果被继承 ...
- poj 1258 Agri-Net prim模板 prim与dijkstra的区别
很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...
- matlab实现算术编解码 分类: 图像处理 2014-06-01 23:01 357人阅读 评论(0) 收藏
利用Matlab实现算术编解码过程,程序如下: clc,clear all; symbol=['abc']; pr=[0.4 0.4 0.2]; %各字符出现的概率 temp=[0.0 0.4 0.8 ...