Python学习笔记 -- 序列(四)元组

本文介绍了Python中的元组数据类型,包括元组的定义、创建方式、特性及其与列表的主要区别。此外,还详细讲解了元组的操作方法,如索引访问、切片、运算符使用及比较等内容。

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
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值