python——tuple元组
>>> dir(tuple)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> tuple
<type 'tuple'>
>>> tuple_1=(1,2,3,4,5,6)
>>> tuple_1
(1, 2, 3, 4, 5, 6)
>>> tuple[0]=1 Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
tuple[0]=1
TypeError: 'type' object does not support item assignment
>>>
tuple就是元组的类型,他和列表相近,但是也有很大的区别,如上,tuple不能修改元素,如果修改就会报错,如上
判断一个列表可以看中括号,但是看元组的依据主要是逗号
eg:如果我们只是需要一个元素,如果不加上逗号,那么会被默认为一个整数类型,如果在后面加上一个逗号,就会被认为是tuple
>>> tuple_2=(1)
>>> tuple_2
1
>>> type(tuple_2)
<type 'int'>
>>> tuple_3=(1,)
>>> tuple_3
(1,)
>>> type(tuple_3)
<type 'tuple'>
>>>
但是对于空的加不加逗号就不影响了
>>> list1=[]
>>> type(list1)
<type 'list'>
>>> tuple1=()
>>> type(tuple1)
<type 'tuple'>
>>>
因为元组不能更改,所有列表里面的3中添加和删除都不能使用
但是我们可以通过切片进行增加和删除
>>>
>>> tmp1=(1,2,4,5,6)
>>> tmp1=tmp1(:2)
SyntaxError: invalid syntax
>>> tmp1=tmp1[:2]+tmp[3:] Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
tmp1=tmp1[:2]+tmp[3:]
NameError: name 'tmp' is not defined
>>> tmp1=tmp1[:2]+tmp1[3:]
>>> tmp1
(1, 2, 5, 6)
>>> tmp1=tmp1[:2]+(4,) #这里注意加上的单个元素一定要加上逗号
>>> tmp1
(1, 2, 4)
>>>
python——tuple元组的更多相关文章
- Python tuple 元组
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 ...
- Python Tuple(元组) tuple()方法
描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- ...
- Python Tuple(元组) min()方法
描述 Python 元组 min() 函数返回元组中元素最小值.高佣联盟 www.cgewang.com 语法 min()方法语法: min(tuple) 参数 tuple -- 指定的元组. 返回值 ...
- Python Tuple(元组) max()方法
描述 Python 元组 max() 函数返回元组中元素最大值.高佣联盟 www.cgewang.com 语法 max()方法语法: max(tuple) 参数 tuple -- 指定的元组. 返回值 ...
- Python Tuple(元组) len()方法
描述 Python 元组 len() 函数计算元组元素个数.高佣联盟 www.cgewang.com 语法 len()方法语法: len(tuple) 参数 tuple -- 要计算的元组. 返回值 ...
- Python tuple元组学习
1.tuple和list非常类似,但是tuple一旦初始化就不能修改 classmates = ('Michael', 'Bob', 'Tracy') 现在,classmates这个tuple不能变了 ...
- Python Tuple元组的操作说明
Tuple的特性在于,它的元素是不可变的(immutable),一旦设定,就不能使用索引去修改. >>> t1=1,2,3,4,5 #给Tuple赋值 >>> t1 ...
- Python Tuple(元组) cmp()方法
描述 Python 元组 cmp() 函数用于比较两个元组元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cmp(tuple1, tuple2) 参数 tuple1 -- ...
- Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...
随机推荐
- PInvoke和Marshal的姿势
PInvoke http://www.mono-project.com/docs/advanced/pinvoke/ https://msdn.microsoft.com/en-us/library/ ...
- thinkphp 语言包丢失
Thinkphp 语言包丢失 造成的原因有那些 1.复制模板 预览时内容出现英文状态 如:show.html 解决:找到lang ,在zh-cn 复制想对应的文件包 改下名称就有可以 如:admin_ ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- OC-Q&A
How to declare a string in Objective-C ? A C string is just like in C. char myCString[] = "test ...
- Orchard源码分析(6):Shell相关
概述在Orchard中,提出子站点(Tenant)的概念,目的是为了增加站点密度,即一个应用程序域可以有多个子站点. Shell是子站点(Tenant)级的单例,换句话说Shell代表了子站点.对比来 ...
- 获取实体属性名称(Property)和DisplayName属性名称(Attribute)
代码: public Dictionary<string, string> XXXModelColumnName { get { var dic = new Dictionary<s ...
- Bootstrap学习------Tabel
Bootstrap的表格和Html表格大同小异,只是封装了一些css供我们使用 <table>标签必须引用class="table"基类样式,我们可以根据需求赛选需要的 ...
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- zend studio 的注册码-php的编辑器
zend studio 12.5 patch包: 组织名(倒过来写)+ jar包名称 : com.zend.verifier-xxx.jar 将破解包中的jar包 覆盖原来就有的那个 verifier ...
- What is the most common software of data mining? (整理中)
What is the most common software of data mining? 1 Orange? 2 Weka? 3 Apache mahout? 4 Rapidminer? 5 ...