首页 > 编程知识 正文

在 Python 中反转元组

时间:2023-11-26 13:13:13 阅读:309395 作者:HAXM

我们知道元组是 Python 中存在的数据结构,其中不同数据类型的元素可以包含在括号中。

在本教程中,我们将学习如何在 Python 中反转元组。

考虑以下例子来理解我们的目标-


Input - (21, 54, 1, 'apple', 'fruits', 11.7)
Output - (11.7, 'fruits', 'apple', 1, 54, 21)

解释-

我们可以在这里观察到,因为我们已经反转了元组的元素,所以最后出现的浮点值排在第一位。

让我们再看一个例子-


Input - (20, 30, 50, 19.3, 41, 'comic books')
Output - ('comic books', 41, 19.3, 50, 30, 20)

解释-

我们可以在这里观察到,因为我们已经反转了元组的元素,所以最后出现的字符串值排在第一位。

我们将使用下面的方法来反转 Python 中的元组,

  1. 使用切片
  2. 使用反向()方法
  3. 在 Python 中使用生成器
  4. 使用索引技术

那么,让我们从第一个开始:

使用切片

Python 中用于对特定范围内的元素进行切片的切片技术。

下面的程序说明了如何使用它们。


#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#using slicing
tuple_values = tuple_values[::-1]
#displaying the tuple
print("The reversed tuple is: ", tuple_values)

输出:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)
The reversed tuple is:  (10, 77, 23.4, 'Java', 'Python', 2, 1)

解释-

是时候看看上面程序的解释了-

  1. 在第一步中,我们已经用不同的值初始化了元组。
  2. 在此之后,我们显示了我们的元组,然后通过将步骤指定为-1,使用切片以相反的顺序打印元组的元素。
  3. 最后,我们显示了反向元组。

在第二个程序中,我们将学习如何使用方法。

使用反向()方法

考虑下面给出的程序,


#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#using slicing
tuple_values = tuple(reversed(tuple_values))
#displaying the tuple
print("The reversed tuple is: ", tuple_values)

输出:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)
The reversed tuple is:  (10, 77, 23.4, 'Java', 'Python', 2, 1)

解释-

让我们了解一下在上面的程序中发生了什么,

  1. 在第一步中,我们已经用不同的值初始化了元组。
  2. 在此之后,我们显示了我们的元组,然后使用 reversed() 以相反的顺序打印元组的元素。
  3. 最后,我们显示了反向元组。

在第三个程序中,我们将看到生成器如何用于同样的目的。

在 Python 中使用生成器

下面给出的程序演示了如何在我们的 Python 程序中使用生成器。


#initializing the tuple
tuple_values = (1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#creating a function
def reverse_tup(tuple_values):
    for i in reversed(range(len(tuple_values))):
        yield tuple_values[i]
#displaying the items in reverse order
for i in reverse_tup(tuple_values):
    print("The element of a reversed tuple are: ", i)

输出:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)
The elements of a reversed tuple are:  10
The elements of a reversed tuple are:  77
The elements of a reversed tuple are:  23.4
The elements of a reversed tuple are:  Java
The elements of a reversed tuple are:  Python
The elements of a reversed tuple are:  2
The elements of a reversed tuple are:  1

解释-

是时候看一看解释了,

  1. 在第一步中,我们已经用不同的值初始化了元组。
  2. 在这之后,我们已经显示了我们的元组,然后创建了一个函数,该函数将元组作为其参数,并使用生成器的概念帮助我们以相反的顺序获得元组。
  3. 最后,我们显示了反向元组。

最后,我们将了解索引如何帮助我们实现目标。

使用索引技术

下面的程序说明了如何做到这一点


#initializing the tuple
tuple_values=(1, 2, 'Python', 'Java', 23.4, 77, 10)
#displaying the original tuple
print("The original tuple is: ", tuple_values)
#displaying the items in reverse order
for i in range(len(tuple_values)):
    print("The element of a reversed tuple is: ", tuple_values[-(i+1)])

输出:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)
The element of a reversed tuple is:  10
The element of a reversed tuple is:  77
The element of a reversed tuple is:  23.4
The element of a reversed tuple is:  Java
The element of a reversed tuple is:  Python
The element of a reversed tuple is:  2
The element of a reversed tuple is:  1

解释-

是时候看看上面程序的解释了-

  1. 在第一步中,我们已经用不同的值初始化了元组。
  2. 在此之后,我们显示了我们的元组,然后创建了一个函数,该函数接受元组的元素,使用 for循环遍历元组,然后通过将索引指定为-1 以相反的顺序打印元素。
  3. 最后,我们显示了反向元组。

结论

在本教程中,我们讨论了在 Python 中反转元组的不同方法。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。