Python 简明教程

Python - Update Tuples

Updating Tuples in Python

在 Python 中, tuple 是不可变序列,意味着一旦创建元组,其元素便不能更改、添加或删除。

In Python, tuple is an immutable sequence, meaning once a tuple is created, its elements cannot be changed, added, or removed.

要在 Python 中更新元组,可以组合各种操作来创建一个新元组。例如,可以连接元组、对它们进行切片,或使用元组解包来实现所需结果。这通常涉及将元组转换为列表,对其进行必要的修改,然后将其转换回元组。

To update a tuple in Python, you can combine various operations to create a new tuple. For instance, you can concatenate tuples, slice them, or use tuple unpacking to achieve the desired result. This often involves converting the tuple to a list, making the necessary modifications, and then converting it back to a tuple.

Updating Tuples Using Concatenation Operator

Python 中的连接运算符表示为 +,用于将两个序列(如字符串、列表或元组)连接成一个序列。当应用于元组时,连接运算符会将两个(或更多)元组的元素连接起来,创建一个包含这两个元组中所有元素的新元组。

The concatenation operator in Python, denoted by +, is used to join two sequences, such as strings, lists, or tuples, into a single sequence. When applied to tuples, the concatenation operator joins the elements of the two (or more) tuples to create a new tuple containing all the elements from both tuples.

我们可以通过使用连接运算符创建一个将原始元组与附加元素相结合的新元组来更新元组。

We can update a tuple using the concatenation operator by creating a new tuple that combines the original tuple with additional elements.

Example

在以下示例中,我们使用“+”运算符将“T1”与“T2”连接起来,创建一个新的元组:

In the following example, we create a new tuple by concatenating "T1" with "T2" using the "+" operator −

# Original tuple
T1 = (10, 20, 30, 40)
# Tuple to be concatenated
T2 = ('one', 'two', 'three', 'four')
# Updating the tuple using the concatenation operator
T1 = T1 + T2
print(T1)

它将生成如下输出:

It will produce the following output −

(10, 20, 30, 40, 'one', 'two', 'three', 'four')

Updating Tuples Using Slicing

在 Python 中切片用于通过指定一系列索引来提取序列(比如列表、元组或字符串)的一部分。切片的语法如下:−

Slicing in Python is used to extract a portion of a sequence (such as a list, tuple, or string) by specifying a range of indices. The syntax for slicing is as follows −

sequence[start:stop:step]

其中,

Where,

  1. start is the index at which the slice begins (inclusive).

  2. stop is the index at which the slice ends (exclusive).

  3. step is the interval between elements in the slice (optional).

我们可以通过创建一个新的元组(包含原始元组的切片和新元素)来使用切片更新元组。

We can update a tuple using slicing by creating a new tuple that includes slices of the original tuple combined with new elements.

Example

在此示例中,我们通过将元组切成两个部分,并在切片之间插入新元素来更新元组,如下所示:

In this example, we are updating a tuple by slicing it into two parts and inserting new elements between the slices −

# Original tuple
T1 = (37, 14, 95, 40)
# Elements to be added
new_elements = ('green', 'blue', 'red', 'pink')
# Extracting slices of the original tuple
# Elements before index 2
part1 = T1[:2]
# Elements from index 2 onward
part2 = T1[2:]
# Create a new tuple
updated_tuple = part1 + new_elements + part2
# Printing the updated tuple
print("Original Tuple:", T1)
print("Updated Tuple:", updated_tuple)

以下是上面代码的输出: -

Following is the output of the above code −

Original Tuple: (37, 14, 95, 40)
Updated Tuple: (37, 14, 'green', 'blue', 'red', 'pink', 95, 40)

Updating Tuples Using List Comprehension

Python 中的列表解析是一种创建列表的简洁方式。它允许您通过将表达式应用于现有可迭代对象中的每个项目来生成新列表(例如列表、元组或字符串),还可以选择性地包含一个条件来筛选元素。

List comprehension in Python is a concise way to create lists. It allows you to generate new lists by applying an expression to each item in an existing iterable, such as a list, tuple, or string, optionally including a condition to filter elements.

由于元组是不可变的,因此更新元组涉及将其转换为列表,使用列表解析进行所需的更改,然后再将其转换为元组。

Since tuples are immutable, updating a tuple involves converting it to a list, making the desired changes using list comprehension, and then converting it back to a tuple.

Example

在下面的示例中,我们首先将元组转换为列表,然后使用列表解析向每个元素中添加 100。然后我们再将列表转换为元组以获取更新后的元组,如下所示:

In the example below, we are updating a tuple by first converting it to a list and using list comprehension to add 100 to each element. We then convert the list back to a tuple to get the updated tuple −

# Original tuple
T1 = (10, 20, 30, 40)
# Converting the tuple to a list
list_T1 = list(T1)
# Using list comprehension
updated_list = [item + 100 for item in list_T1]
# Converting the updated list back to a tuple
updated_tuple = tuple(updated_list)
# Printing the updated tuple
print("Original Tuple:", T1)
print("Updated Tuple:", updated_tuple)

上述代码的输出如下:

Output of the above code is as follows −

Original Tuple: (10, 20, 30, 40)
Updated Tuple: (110, 120, 130, 140)

Updating Tuples Using append() function

append() 函数用于向列表的末尾添加单个元素。然而,由于元组是不可变的,因此无法直接使用 append() 函数来更新元组。

The append() function is used to add a single element to the end of a list. However, since tuples are immutable, the append() function cannot be directly used to update a tuple.

要使用 append() 函数更新元组,我们需要先将元组转换为列表,然后使用 append() 添加元素,最后再将列表转换为元组。

To update a tuple using the append() function, we need to first convert the tuple to a list, then use append() to add elements, and finally convert the list back to a tuple.

Example

在下面的示例中,我们首先将原始元组“T1”转换为列表“list_T1”。然后使用一个循环来迭代新元素,并使用 append() 函数将它们逐个追加到列表中。最后,我们再将更新后的列表转换为元组以获取更新后的元组,如下所示:

In the following example, we first convert the original tuple "T1" to a list "list_T1". We then use a loop to iterate over the new elements and append each one to the list using the append() function. Finally, we convert the updated list back to a tuple to get the updated tuple −

# Original tuple
T1 = (10, 20, 30, 40)
# Convert tuple to list
list_T1 = list(T1)
# Elements to be added
new_elements = [50, 60, 70]
# Updating the list using append()
for element in new_elements:
    list_T1.append(element)
# Converting list back to tuple
updated_tuple = tuple(list_T1)
# Printing the updated tuple
print("Original Tuple:", T1)
print("Updated Tuple:", updated_tuple)

我们得到了如下输出 −

We get the output as shown below −

Original Tuple: (10, 20, 30, 40)
Updated Tuple: (10, 20, 30, 40, 50, 60, 70)