Python 简明教程

Python - Tuple Methods

元组是 Python 中的基本数据结构之一,它是一个不可变序列。与列表不同,元组在创建后不能被修改,因此非常适合表示固定数据集合。它可以包含不同类型的数据元素,例如整数、浮点数、字符串,甚至其他元组。

Tuple is one of the fundamental data structures in Python, and it is an immutable sequences. Unlike lists, tuples cannot be modified after creation, making them ideal for representing fixed collections of data. This immutability play a crucial role in various scenarios where data stability and security are important. It can contain elements of different data types, such as integers, floats, strings, or even other tuples.

Python Tuple Methods

元组类提供了少量的方法来分析数据或元素。这些方法允许用户检索有关元组中特定项的发生情况及其各自索引的信息。由于它是不可变的,因此此类没有定义用于添加或删除项目的方法。它仅定义了两个方法,这些方法提供了一种分析元组数据的方式。

The tuple class provides few methods to analyze the data or elements. These methods allows users to retrieve information about the occurrences of specific items within a tuple and their respective indices. Since it is immutable, this class doesn’t define methods for adding or removing items. It defines only two methods and these methods provide a convenient way to analyze tuple data.

Listing All the Tuple Methods

要浏览元组的所有可用方法,可以使用 Python dir() 函数,它列出了与类相关的所有属性和函数。此外,help() 函数为每个方法提供详细的文档。下面是一个示例:

To explore all available methods for tuples, you can utilize the Python dir() function, which lists all properties and functions related to a class. Additionally, the help() function provides detailed documentation for each method. Here’s an exampl:

print(dir((1, 2)))
print(help((1, 2).index))

上面的代码段提供了与元组类相关的所有属性和函数的完整列表。它还演示了如何在 Python 环境中访问特定方法的详细文档。以下是输出 −

The above code snippet provides a complete list of properties and functions related to the tuple class. It also demonstrates how to access detailed documentation for a specific method in your Python environment. Here is the output −

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.tuple inst ance
    Return first index of value.

    Raises ValueError if the value is not present.
(END)

以下是元组的内置方法。让我们浏览每种方法的基本功能 −

Below are the built-in methods for tuples. Let’s explore each method’s basic functionality −

Finding the Index of a Tuple Item

元组类的 index() 方法返回给定项的第一次出现的索引。

The index() method of tuple class returns the index of first occurrence of the given item.

Syntax

tuple.index(obj)

Return value

index() 方法返回一个整数,表示 “obj” 的第一次出现的索引。

The index() method returns an integer, representing the index of the first occurrence of "obj".

Example

请看以下示例:

Take a look at the following example −

tup1 = (25, 12, 10, -21, 10, 100)
print ("Tup1:", tup1)
x = tup1.index(10)
print ("First index of 10:", x)

它将生成以下 output

It will produce the following output

Tup1: (25, 12, 10, -21, 10, 100)
First index of 10: 2

Counting Tuple Items

元组类中的 count() 方法返回给定对象在元组中出现的次数。

The count() method in tuple class returns the number of times a given object occurs in the tuple.

Syntax

tuple.count(obj)

Return Value

对象的出现次数。count() 方法返回一个整数。

Number of occurrence of the object. The count() method returns an integer.

Example

tup1 = (10, 20, 45, 10, 30, 10, 55)
print ("Tup1:", tup1)
c = tup1.count(10)
print ("count of 10:", c)

它将生成以下 output

It will produce the following output

Tup1: (10, 20, 45, 10, 30, 10, 55)
count of 10: 3

Example

即使元组中的项包含表达式,也会对它们进行求值以获得计数。

Even if the items in the tuple contain expressions, they will be evaluated to obtain the count.

tup1 = (10, 20/80, 0.25, 10/40, 30, 10, 55)
print ("Tup1:", tup1)
c = tup1.count(0.25)
print ("count of 10:", c)

它将生成以下 output

It will produce the following output

Tup1: (10, 0.25, 0.25, 0.25, 30, 10, 55)
count of 10: 3