python 反转列表

The list is a popular structure or composite data type used in Python programming language. One of the most used functions or operations with a list is reversing the list items. Reversing the list items will make the last item as the first item vice versa. In order to reverse a list in Python, there are different functions and methods like reverse() function, slicing, reversed() function with the loop.

该列表是Python编程语言中常用的结构或复合数据类型。 列表最常用的功能或操作之一是反转列表项。 反转列表项将使最后一项成为第一项,反之亦然。 为了在Python中反转列表,循环中有不同的函数和方法,例如reverse()函数,切片,reversed()函数。

(reverse() Function Of List)

List data type provides the reverse() function which is the most practical way to reverse items in a list. reverse() function does not need any parameter as it will use the list object items and put the reversed items in the current list too. In the following example, we will use the numbers as list items in order to depict the reversing operation. The items in the list named numbers will start from 1 to 9 .

列表数据类型提供了reverse()函数,这是反转列表中项目的最实用方法。 reverse()函数不需要任何参数,因为它将使用列表对象项并将颠倒的项也放入当前列表中。 在下面的示例中,我们将数字用作列表项,以描述反转操作。 列表中名为numbers的项目将从1到9开始。

numbers=[1,2,3,4,5,6,7,8,9]

print("Normal List",numbers)
Normal List [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers.reverse()

print("Reversed List",numbers)
Reversed List [9, 8, 7, 6, 5, 4, 3, 2, 1]

numbers.reverse()

print("Normal List Again",numbers)
Normal List Again [1, 2, 3, 4, 5, 6, 7, 8, 9]


python函数列表 python函数列表反转_数据分析

reverse() Function Of List reverse()列表函数

We can see that when we call the reverse() function the reversed numbers are automatically stored to the same list named numbers.

我们可以看到,当我们调用reverse()函数时,反转的数字会自动存储到名为numbers的相同列表中。

(Reverse Using A List Using Slicing Operator)

Python list provides a lot of useful operators were one of them is a slicing operator which is used to select different list items in different ways. We can also use the slicing operator in order to reverse the given list items. The slicing operation will return a new list with reversed items and should be set as a new list which will be more clear.

Python list提供了很多有用的运算符,其中之一是切片运算符,用于以不同的方式选择不同的列表项。 我们还可以使用切片运算符来反转给定的列表项。 切片操作将返回带有反转项目的新列表,应将其设置为新列表,这样会更清晰。

numbers=[1,2,3,4,5,6,7,8,9]

print("Normal List",numbers)
#Normal List [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers_reversed = numbers[::-1]

print("Reversed Numbers",numbers_reversed)
#Reversed Numbers [9, 8, 7, 6, 5, 4, 3, 2, 1]

numbers_reversed_reversed = numbers_reversed[::-1]

print("Reversed Reversed Numbers",numbers_reversed_reversed)
#Reversed Reversed Numbers [1, 2, 3, 4, 5, 6, 7, 8, 9]


python函数列表 python函数列表反转_python函数列表_02

Reverse Using A List Using Slicing Operator 使用切片运算符反向使用列表

具有For循环的反向功能(Reversed Function with For Loop)

Python provides the built-in function named reversed which will return an iterator which will provide a given list in reverse order. We can use this function in order to create a generator. In this example, we will create a reversed generator for numbers with the name of numbers_reversed and enumerate with a for loop.

Python提供了名为reversed的内置函数,该函数将返回一个迭代器,该迭代器将以相反的顺序提供给定的列表。 我们可以使用此功能来创建生成器。 在本示例中,我们将为名称为numbers_reversed numbers创建一个反向生成器,并使用for循环枚举。

numbers=[1,2,3,4,5,6,7,8,9]

numbers_reversed = reversed(numbers)

print("Normal Numbers",numbers)
#Normal Numbers [1, 2, 3, 4, 5, 6, 7, 8, 9]

print("Reversed Numbers",numbers_reversed)
#Reversed Numbers <list_reverseiterator object at 0x7f3fd464a2b0>

for i in numbers_reversed:
  print(i)


python函数列表 python函数列表反转_列表_03

Reversed Function with For Loop 具有For循环的反向功能

We can see that the reversed() function returns an iterator which can be used with different iteration keywords like for. When we try to print the numbers_reversed we get a string that prints the type of the numbers_reversed variable as a list_reverseiterator object.

我们可以看到reversed()函数返回了一个迭代器,该迭代器可以与for等不同的迭代关键字一起使用。 当我们尝试打印numbers_reversed我们得到一个字符串,该字符串将numbers_reversed变量的类型打印为list_reverseiterator对象。