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()列表函数 (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]
We can see that when we call the