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的更多相关文章

  1. c# 元组Tuple

    Tuple类型像一个口袋,在出门前可以把所需的任何东西一股脑地放在里面.您可以将钥匙.驾驶证.便笺簿和钢笔放在口袋里,您的口袋是存放各种东西的收集箱.C# 4.0引入的一个新特性 Tuple类型与口袋 ...

  2. Python—元组tuple

    列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...

  3. Python中的元组(tuple)、列表(list)、字典(dict)

    -------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...

  4. Java元组Tuple使用实例--转载

    原文地址:http://50vip.com/35.html 一.为什么使用元组tuple? 元组和列表list一样,都可能用于数据存储,包含多个数据:但是和列表不同的是:列表只能存储相同的数据类型,而 ...

  5. Python - 元组(tuple) 详解 及 代码

    元组(tuple) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967 元组是存放任意元素集合,不能修 ...

  6. 使用元组Tuple,返回多个不同类型的值

    记得我在不知道Tuple时,如果想实现调用某个函数时,返回多个值,则需要使用ref或者out了. string name = ""; int result= GetInfo(ref ...

  7. Python TypeError: not all arguments converted during string formatting ——元组tuple(a)和(a,)的区别

    今天写程序,想输出一个array的shape,原程序为: print('shape of testUImatrix:%s\nStart to make testUImatrix...'%(testui ...

  8. Python元组(tuple)

    元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...

  9. Java元组Tuple介绍与使用

    一.元组介绍 仅仅一次方法调用就可以返回多个对象,你应该经常需要这样的功能吧.可以return语句只允许返回单个对(可能有人说返回一个集合就可以了,请记住,一个集合也只是一个对象而已)因此,解决办法就 ...

  10. python基础之列表list元组tuple

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7041763.html python基础之列表list元组tuple 列表li ...

随机推荐

  1. 深入浅出Java多线程

    Java给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 多线程是多任务的一种特别的形式,但多线程使用了 ...

  2. Vue从零开始(一)

    一.什么是Vue? Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层 ...

  3. android 设置textview跑马灯效果

    head_tv1.setEllipsize(TextUtils.TruncateAt.MARQUEE);head_tv1.setSingleLine(true);head_tv1.setSelecte ...

  4. Gulp-前端进阶A-1

    毕业到转行以来有一年时间了,成为一名程序猿也有大半年了,之前在新浪上随便写写简单的学习过程,感觉不够像那么回事,现在接触前端也有一段时间了,也做过几个项目,认识到可以拓展的实在太多了,希望从这里起步, ...

  5. 实用技巧:使用 jQuery 异步加载 JavaScript 脚本

    JavaScript 加载器在 Web 开发中是非常强大和有用的工具.目前流行的几个加载器,像 curljs.LABjs 和 RequireJS 使用都很广泛.他们功能强大的,但有些情况下可以有更简单 ...

  6. Web 开发人员不能错过的 jQuery 教程和案例

    jQuery 把惊喜延续到设计领域,处处带来极大的灵活性,创造了许多体验良好的设计,而且拥有不错的性能.这里分享一组 Web 开发人员不能错过的 jQuery 教程和案例,帮助你更好的掌握 jQuer ...

  7. JS的window.location应用实例

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Locationwindow.location 对象在编写时可不使用 window ...

  8. [转]maven创建自定义的archetype

    创建自己的archetype一般有两种方式,比较简单的就是create from project 1.首先使用eclipse创建一个新的maven project,然后把配置好的一些公用的东西放到相应 ...

  9. 在线代码编辑器CodeMirror简介

    1.什么是Code Mirror 最近做一个项目需要在网页上实现一个代码编辑器,支持语法高亮.自动缩进.智能提示等功能.发现Code Mirror刚好满足所有需求.Code Mirror是由js写的一 ...

  10. 谈谈JavaScript类型检测

      javascript内置的类型检测机制并非完全可靠.比如typeof操作符,并不能准确的判断数据是哪个类型,比如:数组和对象就不能通过typeof来区分. typeof [] ==="o ...