Python 元组——使用元组

位置:首页>文章>详情   分类: Python教程 > 编程技术   阅读(329)   2024-05-15 10:56:16

In 皮顿, a 元组 is similar to 列表 except it is 不变的 and are written with optional 圆括号.

元组是:

  • 不变的
  • 有序的
  • 异质
  • 索引(从零开始)
  • 用圆括号书写(可选但推荐)
  • 在迭代过程中更快,因为它是不可变的

元组用于创建对象很有用,它通常包含相关信息,例如员工信息。换句话说,元组让我们将相关信息“组合”在一起,并将其作为单一事物使用。

1.创建一个元组

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

Tuple = (item1, item2, item3)
tuple1 = ()		# empty tuple

tuple2 = (1, "2", 3.0)

tuple3 = 1, "2", 3.0

1.1.一个元素的元组

如果元组只包含一个元素,则它不被视为元组。它应该有一个尾随逗号来指定解释器它是一个元组。

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

1.2.嵌套元组

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

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

2.访问元组项

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

  • 正指数 begins counting from start of tuple.
  • 负指数 begins counting from end of tuple.
  • A 指标范围 will create a new tuple (called 切片) with the specified items.
  • Range [米:n] means from position m (包括的) to position 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.循环成元组

使用 for循环 遍历元组项。

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

for x in Tuple:
  print(x)

4.检查一个项目是否存在于元组中

要检查元组是否包含给定元素,我们可以使用 '在' 关键字和 '不在' 关键字。

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. 对元组进行排序

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

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

sortedTuple = sorted(Tuple)

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

6. 元组的重复和连接

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

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. 打包和解包元组

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

在下面的示例中,所有三个值都分配给变量 元组

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

开箱是指将元组变量分配给另一个元组并将元组中的各个项目分配给各个变量的操作。

在给定的示例中,元组被解压缩为新的元组,并且值 “a”、“b”和“c” – 被分配给变量 x、y 和 z

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

(x, y, z) = Tuple

print (x)	# a
print (y)	# b
print (z)	# c

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

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.命名元组

Python provides a special type of function called 命名元组() that comes from the 收藏 module.

命名元组类似于字典,但支持从值和键访问,其中字典仅支持通过键访问。

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 元组方法

9.1.任何()

如果元组中至少存在一个元素,则返回 正确的,如果元组为空,则返回错误的

print( any( () ) )		# Empty tuple - False
print( any( (1,) ) )	# One element tuple - True
print( any( (1, 2) ) )	# Regular tuple - True

9.2.分钟()

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

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

print( min( Tuple ) )	# 1

9.3.最大限度()

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

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

print( max( Tuple ) )	# 9

9.4.长度()

返回元组的长度。

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

print( len( Tuple ) )	# 5

9.5.和()

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

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

print( sum( Tuple ) )	# 22

10.结论

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

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

元组支持类似列表类型的操作,只是我们不能改变元组元素。

快乐学习!!

阅读更多 :

Python – 元组比较 Python——列表与元组

地址:https://www.cundage.com/article/python-tuples.html

相关阅读

Python 集是独特元素的无序集合。了解 Python 中的 Set datatype,创建和修改 Set 以及其他有用的 Set 操作。 1. 什么是集合 Python 中的集合是: 独特的...
Python bin() 方法将给定的 integer 转换为等效的二进制 < href="https://howtodoinjava.com/python-datatypes/pytho...
OrderedDict 维护添加到字典中的项目的插入顺序。在迭代或序列化时也会保留项目的顺序。 1.Python OrderedDict例子 OrderedDict 是 python 集合模块的...
在 Python 中,列表是: 有序的 索引(索引从 0 开始) 可变的 异构(列表中的项目可以是不同类型) 写成方括号之间以逗号分隔的值列表 1.创建列表 以下是在 Python 中创建列表的...
学习使用 Python httplib2 模块。 超文本传输协议 (HTTP) 是分布式协作超媒体信息系统的应用协议。 HTTP 是万维网数据通信的基础。 Python httplib2 模块提...
阅读、理解和练习这些 Python 示例,以更好地理解 Python 语言。这些简单的 Python 程序将帮助我们理解 Python 的基本编程概念。 此页面上的所有程序都经过测试,应该可以在...
阅读、理解和练习这些 Python 示例,以更好地理解 Python 语言。这些简单的 Python 程序将帮助我们理解 Python 的基本编程概念
1. Python整数值 在 Python 中,int 或整数是: 一个不带小数的整数 正、负或零 无限长 可以包含下划线以提高可读性 x = 10 y = 123456789876543...
在 Python 中,列表是: 有序的 索引(索引从 0 开始) 可变的 异构(列表中的项目可以是不同类型) 写成方括号之间以逗号分隔的值列表
1. Python 内置函数 蟒蛇绝对() 返回整数的绝对值,float;和复数的大小。 Python any() 函数 检查 Iterable 的至少一个元素是否为 True。 Python ...