Skip to content

Latest commit

 

History

History
288 lines (164 loc) · 5.95 KB

File metadata and controls

288 lines (164 loc) · 5.95 KB

第5节 python元组(tuple)

❤️💕💕python是一种动态的解释形语言,由于python的普遍性,学会python能更快的解决问题,以及学习其他的知识。Myblog:http:https://nsddd.top


[TOC]

python元组

#创建一个元组:
thistuple = ("apple", "banana", "cherry")
print(thistuple)

元组项是有序的、不可更改的,并且允许重复值。

元组项被索引,第一项具有索引[0],第二项具有索引[1]等。

有序的

当我们说元组是有序的时,这意味着项目具有定义的顺序,并且该顺序不会改变。

不可改变的

元组是不可更改的,这意味着在创建元组后我们不能更改、添加或删除项目。

允许重复

由于元组被索引,它们可以有具有相同值的项目:

案例

In [145]: a = (12,31,4,1,3,3)

In [146]: type(a)
Out[146]: tuple

In [147]: a
Out[147]: (12, 31, 4, 1, 3, 3)   # 允许重复值

长度

In [148]: len(a)
     ...:
     ...:
Out[148]: 5

创建具有一个项目的元组

要创建一个只有一个项目的元组,你必须在项目后添加一个逗号,否则 Python 不会将其识别为一个元组。

In [149]: thistuple = ("apple",)
     ...: print(type(thistuple))
     ...:
     ...: #NOT a tuple
     ...: thistuple = ("apple")
     ...: print(type(thistuple))
<class 'tuple'>
<class 'str'>

In [154]: thistuple = ()

In [155]: type(thistuple)
Out[155]: tuple

元组的数据类型

元组项可以是任何数据类型,而且元组项可以包含不同的数据类型

字符串、整数和布尔数据类型:

In [154]: thistuple = ()

In [155]: type(thistuple)
Out[155]: tuple

In [156]: tuple1 = ("apple", "banana", "cherry")
     ...: tuple2 = (1, 5, 7, 9, 3)
     ...: tuple3 = (True, False, False)

In [157]: tuple4 = tuple3 + tuple2

In [158]: tuple4
Out[158]: (True, False, False, 1, 5, 7, 9, 3)

In [159]: tuple5 = tuple(("a","b","c")) #构造方式创建元组

In [160]: tuple5
Out[160]: ('a', 'b', 'c')

Python 编程语言中有四种集合数据类型:

  • List是一个有序且可变的集合。允许重复成员。
  • 元组是一个有序且不可更改的集合。允许重复成员。
  • Set是一个无序、不可更改和无索引的集合。没有重复的成员。
  • 字典是一个有序的集合和可变的。没有重复的成员。

访问元组:和列表方法一样

修改元组

因为元组是不可修改的,我们想要修改元组,必须要转化为列表后修改

而且需要注意的是:我们转化了并非是转化的元组,而是需要一个列表来接收

In [161]: tuple4
Out[161]: (True, False, False, 1, 5, 7, 9, 3)

In [162]: list(tuple4) # 注意要接收,不然会报错
Out[162]: [True, False, False, 1, 5, 7, 9, 3]

In [163]: tuple4[1] = 100  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [163], in <cell line: 1>()
----> 1 tuple4[1] = 100

TypeError: 'tuple' object does not support item assignment

In [164]: tuple4
Out[164]: (True, False, False, 1, 5, 7, 9, 3)

In [165]: a = list(tuple4) # 用a接收

In [166]: a
Out[166]: [True, False, False, 1, 5, 7, 9, 3]

In [167]: a[1]=1

In [168]: tuple4 = tuple(a)

In [169]: tuple4
Out[169]: (True, 1, False, 1, 5, 7, 9, 3)

添加项目

元组是不可变的,添加项目有两种方式

  • 把要添加的数放在一个元组中,两个元组相加
  • 转化为list修改

删除元组

del完全删除元组

thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists

解包元组

我们在创建一个元组的时候,通常给它赋值,则称之为打包元组

相反:解包元组就是将值提取回变量中。这称为“解包”:

In [177]: fruits  # 打包好的元组
Out[177]: ('apple', 'banana', 'cherry')

In [178]: (green, yellow, red) = fruits

In [179]: green
Out[179]: 'apple'

In [180]: yellow
Out[180]: 'banana'

In [181]: red
Out[181]: 'cherry'

循环元组

可以用循环遍历的方式循环一个元组

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)

也可以通过引用它们的索引号打印所有项目:

thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
 print(thistuple[i])

使用 While 循环

您可以使用循环遍历列表项while

使用该len()函数确定元组的长度,然后从 0 开始并通过引用它们的索引来循环遍历元组项。

请记住在每次迭代后将索引增加 1。

打印所有项目,使用while循环遍历所有索引号:

thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
 print(thistuple[i])
 i = i + 1

元组相加和相乘

和列表一样,元组也可以相加或者是相乘

In [185]: a = list1 * 2

In [186]: a
Out[186]: ['a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c', 1, 2, 3]

In [187]: fruits
Out[187]: ('apple', 'banana', 'cherry')

In [188]: fruits * 2
Out[188]: ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

END 链接