python中tuple_(Python基础教程之九)Python中的Tuple操作

在Pyhton中,元组类似于不变,list但不可变,并带有可选的圆括号。

元组是:

不可变

有序

异质

索引(从零开始)

带圆括号(可选,但建议)

在迭代过程中更快,因为它是不可变的

元组对于创建通常包含相关信息(例如员工信息)的对象很有用。换句话说,元组可以让我们将相关信息“块”在一起,并将其用作单个事物。

1. Creating a Tuple

元组中的元素用圆括号括起来,并用逗号分隔。元组可以包含任意数量的不同类型的项。

句法

Tuple = (item1, item2, item3)

元组的例子

tuple1 = () # empty tuple

tuple2 = (1, "2", 3.0)

tuple3 = 1, "2", 3.0

1.1. Tuple with one element

如果元组仅包含一个元素,则不将其视为元组。它应该以逗号结尾以指定解释器为元组。

元组的例子

tupleWithOneElement = ("hello", ) # Notice trailing comma

1.2. Nested Tuple

一个包含另一个元组作为元素的元组,称为嵌套元组。

嵌套元组

nestedTuple = ("hello", ("python", "world"))

2. Accessing Tuple Items

我们可以使用方括号内的索引访问元组项。

正索引从元组的开始开始计数。

负索引从元组的末尾开始计数。

一定范围的索引将使用指定的项目创建一个新的元组(称为Slicing)。

范围[m:n]是指从位置m(含)到位置n(_不含_)。

使用双索引访问嵌套元组的元素。

存取项目

Tuple = ("a", "b", "c", "d", "e", "f")

print(Tuple[0]) # a

print(Tuple[1]) # b

print(Tuple[-1]) # f

print(Tuple[-2]) # e

print(Tuple[0:3]) # ('a', 'b', 'c')

print(Tuple[-3:-1]) # ('d', 'e')

Tuple = ("a", "b", "c", ("d", "e", "f"))

print(Tuple[3]) # ('d', 'e', 'f')

print(Tuple[3][0]) # d

print(Tuple[3][0:2]) # ('d', 'e')

3. Loop into tuple

使用for循环遍历元组项。

对于循环示例

Tuple = ("a", "b", "c")

for x in Tuple:

print(x)

4. Check if an item exist in tuple

要检查一个元组是否包含给定的元素,我们可以使用'in'关键词和'not in'关键词。

检查项目是否存在于元组中

Tuple = ("a", "b", "c", "d", "e", "f")

if "a" in Tuple:

print("Yes, 'a' is present") # Yes, 'a' is present

if "p" not in Tuple:

print("No, 'p' is not present") # No, 'p' is not present

5. Sorting a Tuple

使用语言内置sorted()方法对元组内的元素进行排序。

排序元组

Tuple = ("a", "c", "b", "d", "f", "e")

sortedTuple = sorted(Tuple)

print (sortedTuple) # ("a", "b", "c", "d", "e", "f")

6. Repetition and Concatenation of Tuples

要重复元组的所有元素,请将其乘以所需因子N。

重复

Tuple = ("a", "b")

repeatedTuple = Tuple * 3

print (repeatedTuple) # ('a', 'b', 'a', 'b', 'a', 'b')

要连接/连接两个或多个元组,我们可以使用+运算符。

级联

Tuple1 = ("a", "b", "c")

Tuple2 = ("d", "e", "f")

joinedTuple = Tuple1 + Tuple2

print (joinedTuple) # ("a", "b", "c", "d", "e", "f")

7. Packing and Unpacking the Tuple

打包 是指我们为变量分配一组值的操作。在打包时,元组中的所有项目都分配给一个元组对象。

在下面的示例中,所有三个值都分配给了variable Tuple。

Packing

Tuple = ("a", "b", "c")

拆包 称为将元组变量分配给另一个元组,并将元组中的各个项目分配给各个变量的操作。

在给定的示例中,元组被解包为新的元组,并且将值"a", "b" and "c"–分配给了变量x, y and z

Unpacking

Tuple = ("a", "b", "c") # Packing

(x, y, z) = Tuple

print (x) # a

print (y) # b

print (z) # c

在解包期间,分配左侧的元组中的元素数必须等于右侧的数量。

Unpacking errors

Tuple = ("a", "b", "c") # Packing

(x, y, z) = Tuple # ValueError: too many values to unpack (expected 2)

(x, y, z, i) = Tuple # ValueError: not enough values to unpack (expected 4, got 3)

8. Named tuples

Python提供了一种来自模块的特殊类型的函数,名为namedtuple()collection。

命名元组类似于字典,但是支持从值和键进行访问,其中字典仅支持按键访问。

命名元组示例

import collections

Record = collections.namedtuple('Record', ['id', 'name', 'date'])

R1 = Record('1', 'My Record', '12/12/2020')

#Accessing using index

print("Record id is:", R1[0]) # Record id is: 1

# Accessing using key

print("Record name is:", R1.name) # Record name is: My Record

9. Python Tuples Methods

9.1. any()

返回True如果至少一个元素存在于元组,并返回False如果元组是空的。

print( any( () ) ) # Empty tuple - False

print( any( (1,) ) ) # One element tuple - True

print( any( (1, 2) ) ) # Regular tuple - True

9.2. min()

返回元组的最小元素(整数)。

Tuple = (4, 1, 2, 6, 9)

print( min( Tuple ) ) # 1

9.3. max()

返回元组的最大元素(整数)。

Tuple = (4, 1, 2, 6, 9)

print( max( Tuple ) ) # 9

9.4. len()

返回元组的长度。

Tuple = (4, 1, 2, 6, 9)

print( len( Tuple ) ) # 5

9.5. sum()

返回元组的所有元素(整数)的总和。

Tuple = (4, 1, 2, 6, 9)

print( sum( Tuple ) ) # 22

10. Conclusion

如上所述,元组不可变,有序和索引的异构元素集合。它写有或没有圆括号。

元组对于创建对象类型和实例非常有用。

元组支持类似于list类型的操作,只有我们不能更改元组元素。

学习愉快!

作者:分布式编程

出处:https://zthinker.com/

如果你喜欢本文,请长按二维码,关注 分布式编程

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值