Python学习笔记 -- 序列(四)元组
2015-06-12 09:35
686 查看
Python学习笔记 – 序列(四)元组
标签(空格分隔): python 序列元组(tuple),也是是Python中的数据集类型之一。如列表一样,也是序列类型,且和列表非常相近。但是两者存在区别除了看起来很像(列表用”[]”而元组使用”()”)以外,就是元组是不可变类型。所以元组可以做列表不能做的事情,比如元组可以作为字典的key。
>>> aList = [1,2,3] #这是一个列表 >>> aList [1, 2, 3] >>> aTuple = (1,2,3) #这是一个元组 >>> aTuple (1, 2, 3) >>> #列表是可变的 >>> id(aList) 44851328 >>> aList.append(4) >>> aList [1, 2, 3, 4] >>> id(aList ) 44851328 >>> aList = aList + [1,2] >>> id(aList) 33633344 >>> #元组是不可变的 >>> id(aTuple ) 44848032 >>> aTuple.append(4) #由于元组是不可变的,所以它没有append函数 Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> aTuple.append(4) AttributeError: 'tuple' object has no attribute 'append' >>> aTuple = aTuple+(1,2) >>> id(aTuple) #改变了元组,但实际上是创建了新的对象关联(指向了新的对象) 43392672 >>>
元组也是序列,所以也可以通过下标来访问其中的值。当然,其也有切片操作。
>>> aTuple = tuple('abcdef') >>> aTuple ('a', 'b', 'c', 'd', 'e', 'f') >>> aTuple[1] 'b' >>> aTuple[3:5] ('d', 'e') >>> aTuple[:] ('a', 'b', 'c', 'd', 'e', 'f') >>> aTuple[-1:3:1] () >>> aTuple[-2::1] ('e', 'f') >>>
元组的创建
元组的创建及赋值和列表基本完全一样,除了一点就是当只有一个元素时,需要在元组分隔符(即”()”)中加一个逗号,以作为和分组操作符的区别。Python中的元组通过几种方式创建:
通过列表的构造函数tuple来创建
#列表创建 >>> aList = list("List") >>> aList ['L', 'i', 's', 't'] #元组创建 >>> aTuple = tuple("Tuple") >>> aTuple ('T', 'u', 'p', 'l', 'e') >>>
注意,如list()创建列表一样,在用tuple创建元组时传入的参数也应该是序列或可迭代的对象。
>>> aTuple = tuple(12345) Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> aTuple = tuple(12345) TypeError: 'int' object is not iterable >>>
通过元组分隔符()来创建 - - 字面量创建
>>> aTuple = (1,2,3,4,5); >>> aTuple (1, 2, 3, 4, 5) >>> #注意单个字符时分隔符内应该有一个逗号来标示这是个元组 >>> bTuple = ("A") >>> type(bTuple ) <type 'str'> >>> >>> cTuple = ("A",) >>> type(cTuple ) <type 'tuple'> #空元组 >>> nullTuple = () >>> type(nullTuple) <type 'tuple'> #使用tuple创建空元组 >>> nullTuple = tuple() >>> type(nullTuple) <type 'tuple'> >>> len(nullTuple) 0 >>>
通过range来创建整数元组
>>> aTuple = range(1,10) >>> aTuple [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
range()是用于迭代生成一个序列的函数,可以生成等差级数。
range(…)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
start为开始,stop为结束(不取这个值,即左闭右开),step为步长。
元组运算符操作
+和*运算符(重载运算符)运算符 | 作用 |
---|---|
+运算符 | 用于连接元组(只能连接元组) |
*运算符 | 用于重复列表内容 |
# '+'运算符,链接非元组时会报错 >>> aTuple = ("Hello",) >>> bTuple = aTuple+("Python"); Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> bTuple = aTuple+("Python"); TypeError: can only concatenate tuple (not "str") to tuple >>> bTuple = aTuple+("Python",); >>> bTuple ('Hello', 'Python') >>> #'*'运算符 >>> cTuple = bTuple * 3 >>> cTuple ('Hello', 'Python', 'Hello', 'Python', 'Hello', 'Python') >>>
列表操作中+运算符只能连接元组,连接其他内容会报错。
in和not in运算符
运算符 | 作用 |
---|---|
in运算符 | 用于检查一个对象是否在列表中 |
not in运算符 | 用于检查某对象是不在列表中 |
>>> aTuple = tuple('abcde') >>> aTuple ('a', 'b', 'c', 'd', 'e') >>> 'a' in aTuple True >>> 'f' in aTuple False >>> ('a','b') in aTuple False >>>
in和not in在元组中的用法和字符串及列表中一样,均用于检测成员。
元组比较
单个元素比较>>> (2)>(1) #注意单个元素的元组表示 True >>> type((2)) <type 'int'> >>> (2,) > (1,) True >>> type((2,)) <type 'tuple'> >>> ('a',) > (2,) True >>> ('a',) > ('b',) False
单个字符比较中,尤其要注意就是单个字符的表示
元组其他操作
1.检索
T.index(value, [start, [stop]])返回value在元组T中第一个出现的位置(索引),找不到时返回ValueError
异常。
>>> aTuple ('a', 'b', 'c', 'd', 'e') >>> aTuple.index('a') 0 >>> aTuple.index('f') Traceback (most recent call last): File "<pyshell#76>", line 1, in <module> aTuple.index('f') ValueError: tuple.index(x): x not in tuple >>>
2.统计
T.count(value)统计value在元组T中出现的次数。
>>> aTuple = tuple("Hello Python") >>> aTuple.count('o') 2 >>>
相关文章推荐
- 初步了解python
- 搞笑的程序猿:看看你是哪种Python程序员
- 用python操作浏览器的两种方式
- python对目录的操作
- Python 3.x 新特性及10大变化
- 提高Python运行效率的六个窍门
- 跟Tovi学Python3.x(一)
- Python实现比较两个列表(list)范围
- wxpython无标题窗口最大化解决方案
- Python 扩展
- Python实现比较两个列表(list)范围
- Python 3.x 新特性及10大变化
- 搞笑的程序猿:看看你是哪种Python程序员
- Python中unittest模块做UT(单元测试)使用实例
- Python3中的2to3转换工具使用示例
- Windows下实现Python2和Python3两个版共存的方法
- Python2中的raw_input() 与 input()
- python twisted socket 服务端 客户端
- 【转载】Deep Learning Python Tutorial
- 学习Python3(随机数)