元组tuple
tuple和list的主要区别就是tuple里的元素的是不能改变的,即tuple时immutable的
#创建tuple
>>> tupa = (12 , 'ed' , 34)
>>> tupb = 134 , 'hf', 43
>>> tupc = (12 ,) #一个元素的时候不能不写","
>>> print tupa,tupb, tupc
(12, 'ed', 34) (134, 'hf', 43) (12,)
#访问tuple
#tupb[1:2]包括第1个但不包括第2个,Python很多地方都是这样的
>>> print tupa[0],tupb[1:2], tupc[-1] , tupb[-2]
12 ('hf',) 12 hf
#更新tuple
#tuple是immutable的,里面的值不能更改
>>> tupa[0] = 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
#合并tuple
>>> tupd = tupa + tupb
>>> print tupa, tupb, tupd
(12, 'ed', 34) (134, 'hf', 43) (12, 'ed', 34, 134, 'hf', 43)
#删除tuple
#tuple是immutable的,里面的值不能删除,只能使用del删除整个tuple
>>> del tupd[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> del tupd
>>> print tupd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tupd' is not defined
基本操作
#测试tuple里元素的数目
>>> len(tupa)
3
#合并tuple
>>> print tupa+tupb
(12, 'ed', 34, 134, 'hf', 43)
#重复tuple
>>> print tupa*2, tupa[0]*3, tupa[1]*4
(12, 'ed', 34, 12, 'ed', 34) 36 edededed
#元素所属
>>> 12 in tupa
True
>>> 12 in tupa, 14 in tupa
(True, False)
>>> test = 12 in tupa
>>> print test
True
>>> test = 12 in tupa, 15 in tupa
>>> print test
(True, False)
#遍历元素
>>> for x in tupa:
... print x
...
12
ed
34
Built-in Tuple Functions
cmp()
Compares elements of both tuples.If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.
- If numbers, perform numeric coercion if necessary and compare.
- If either element is a number, then the other element is "larger" (numbers are "smallest").
- Otherwise, types are sorted alphabetically by name.
If we reached the end of one of the tuples, the longer tuple is "larger." If we exhaust both tuples and share the same data, the result is a tie, meaning that 0 is returned
Syntax&P
cmp(tuple1, tuple2)
eg
>>> cmp (tupa,tupb)
-1
len()
Returns the total length of the tuple.
Syntax&P
len(tuple)
eg
>>> len(tupa)
3
max()
Returns item from the tuple with max value.
Syntax&P
max(tuple)
eg
>>> max(tupa)
'ed'
min()
Returns item from the tuple with min value.
Syntax&P
min(tuple)
eg
>>> min(tupa)
12
tuple()
Converts a list into tuple
Syntax&P
tuple(seq)
eg
>>> lia=[12,14,'12']
>>> print tuple(lia)
(12, 14, '12')
元组tuple的更多相关文章
- c# 元组Tuple
Tuple类型像一个口袋,在出门前可以把所需的任何东西一股脑地放在里面.您可以将钥匙.驾驶证.便笺簿和钢笔放在口袋里,您的口袋是存放各种东西的收集箱.C# 4.0引入的一个新特性 Tuple类型与口袋 ...
- Python—元组tuple
列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...
- Python中的元组(tuple)、列表(list)、字典(dict)
-------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...
- Java元组Tuple使用实例--转载
原文地址:http://50vip.com/35.html 一.为什么使用元组tuple? 元组和列表list一样,都可能用于数据存储,包含多个数据:但是和列表不同的是:列表只能存储相同的数据类型,而 ...
- Python - 元组(tuple) 详解 及 代码
元组(tuple) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967 元组是存放任意元素集合,不能修 ...
- 使用元组Tuple,返回多个不同类型的值
记得我在不知道Tuple时,如果想实现调用某个函数时,返回多个值,则需要使用ref或者out了. string name = ""; int result= GetInfo(ref ...
- Python TypeError: not all arguments converted during string formatting ——元组tuple(a)和(a,)的区别
今天写程序,想输出一个array的shape,原程序为: print('shape of testUImatrix:%s\nStart to make testUImatrix...'%(testui ...
- Python元组(tuple)
元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...
- Java元组Tuple介绍与使用
一.元组介绍 仅仅一次方法调用就可以返回多个对象,你应该经常需要这样的功能吧.可以return语句只允许返回单个对(可能有人说返回一个集合就可以了,请记住,一个集合也只是一个对象而已)因此,解决办法就 ...
- python基础之列表list元组tuple
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7041763.html python基础之列表list元组tuple 列表li ...
随机推荐
- 通过jQuery的比较来认识AngularJS
通过jQuery的比较来认识AngularJS 这一章主要是通过几个例子分别通过jQuery和AngularJS来达到效果.主要通过思维转换来进一步了解AngularJS框架设计背后的思想. 注意: ...
- SDRAM,DRAM,SRAM,DDR的概念
一:SDRAM SDRAM(Synchronous Dynamic Random Access Memory),同步动态随机存储器,同步是指 Memory工作需要同步时钟,内部的命令的发送与数据的传输 ...
- Guava学习笔记:Guava新增集合类型-Multimap
在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,以便做相应的业 ...
- struts工程建立配置细节
ActionForm ActionForm是用于存放表单提交的数据. 1.一个action是否必须配置一个actionForm,可以没有2.一个action是否可以配置多个表单?不可以,最多一个 在j ...
- Hadoop学习笔记1-如何简单布署hadoop
企业机型配置: 选型标准:普通的,廉价的,标准的(容易替换的),工业化大规模生产的 CPU:支持多核CPU,如2个4核CPU 内存:16G以上,内存越大,常用数据都缓存在内存,提高速度 硬盘:不需RA ...
- ADO.NET操作数据库(一)
---恢复内容开始--- [1]ADO.Net简介2015-12-07-20:16:05 ADO.Net提供对Microsoft SQL Server数据源以及通过OLE DB和XML公开的数据源的一 ...
- 10个漂亮的响应式的食品 WordPress 美食模板
您是否拥有一个餐厅,酒吧,咖啡馆,小酒馆,比萨饼店?如果答案是肯定的,请确保您在网上也提供服务.为了使您的工作更轻松,我们选择了一些新的和独特的餐厅主题,覆盖了范围很广的食品企业.这些主题提供了很多很 ...
- 2013年最新流行的响应式 WordPress 主题【上篇】
WordPress 是最流行的内容管理系统,经历了多年的发展和演变.它的成功体现在庞大的社区,数百万的用户,设计师和开发者推动着 WordPress 往更高的层次发展. 海量的免费主题是 WordPr ...
- 轻松玩转jquery。
一.简介 jQuery创始人是美国John Resig,是优秀的Javascript框架: jQuery是一个轻量级.快速简洁的javaScript库.源码戳这 jQuery产生的对象时jQuery独 ...
- 封装Nvelocity的渲染方法
public class CommonHelper { /// <summary> /// 用data数据填充templatename模板,渲染返回html返回 /// </summ ...