Python 简明教程

Python - Reverse Arrays

数组反转是将数组元素按相反顺序重新排列的操作。在 Python 中,有多种方法和途径可以反转数组,包括 reverse() 和 reversed() 方法。

Reversing an array is the operation of rearranging the array elements in the opposite order. There are various methods and approaches to reverse an array in Python including reverse() and reversed() methods.

在 Python 中, array 不是内置的 data types 。不过,Python 的标准库有一个 array 模块,可以帮助我们创建均质的 string 、整数或浮点数集合。

In Python, array is not one of the built-in data types. However, Python’s standard library has array module which helps us to create a homogenous collection of string, integer or float types.

reverse array operation python

Ways to Reverse an Array in Python

要反转数组,请使用以下方法:

To reverse an array, use the following approaches −

  1. Using slicing operation

  2. Using reverse() method

  3. Using reversed() method

  4. Using for loop

Using slicing operation

切片操作是在指定索引内提取数组一部分的过程。在 Python 中,如果我们使用形式为 [::-1] 的切片操作,它会通过反转原始数组来显示一个新数组。

Slicing operation is the process of extracting a part of array within the specified indices. In Python, if we use the slice operation in the form [::-1] then, it will display a new array by reversing the original one.

在这个过程中,解释器从末尾开始,并向后逐步递减 1,直到到达数组开头。结果,我们得到原始数组的一个反向副本。

In this process, the interpreter starts from the end and stepping backwards by 1 until it reaches the beginning of the array. As a result, we get a reverse copy of original array.

Example

以下示例演示如何在 Python 中使用切片操作来反转数组。

The below example demonstrates how to use the slicing operation to reverse an array in Python.

import array as arr

# creating array
numericArray = arr.array('i', [88, 99, 77, 55, 66])

print("Original array:", numericArray)
revArray = numericArray[::-1]
print("Reversed array:",revArray)

当你运行此代码时,它将会生成以下输出:

When you run the code, it will produce the following output −

Original array: array('i', [88, 99, 77, 55, 66])
Reversed array: array('i', [66, 55, 77, 99, 88])

Reverse an Array Using reverse() Method

我们还可以使用列表类的 reverse() method 反转数组中的数字顺序。这里, list 是 Python 中的一种内置类型。

We can also reverse the sequence of numbers in an array using the reverse() method of list class. Here, list is a built-in type in Python.

因为 reverse() 是列表类的某个方法,所以我们不能直接使用它来反转通过 Python array 模块创建的数组。我们必须先使用 array 类的 tolist() 方法将数组的内容转移到一个列表,然后我们调用 reverse() 方法,最后,当我们将列表转换回数组时,我们得到一个顺序反转了的数组。

Since reverse() is a method of list class, we cannot directly use it to reverse an array created through the Python array module. We have to first transfer the contents of an array to a list with tolist() method of array class, then we call the reverse() method and at the end, when we convert the list back to an array, we get the array with reversed order.

Example

在这里,我们将了解在 Python 中反转数组时如何使用 reverse() 方法。

Here, we will see the use of reverse() method in reversing an array in Python.

import array as arr

# creating an array
numericArray = arr.array('i', [10,5,15,4,6,20,9])
print("Array before reversing:", numericArray)

# converting the array into list
newArray = numericArray.tolist()

# reversing the list
newArray.reverse()

# creating a new array from reversed list
revArray = arr.array('i', newArray)
print ("Array after reversing:",revArray)

它将生成以下 output

It will produce the following output

Array before reversing: array('i', [10, 5, 15, 4, 6, 20, 9])
Array after reversing: array('i', [9, 20, 6, 4, 15, 5, 10])

Reverse an Array Using reversed() Method

reversed() 方法是另一种反转数组元素的方法。它接受一个数组作为参数值,并返回一个迭代器对象,以反向顺序显示数组元素。

The reversed() method is another way to reverse elements of an array. It accepts an array as a parameter value and returns an iterator object that dispalys array elements in reverse order.

Example

在这个示例中,我们使用 reversed() 方法来反转 Python 中的一个数组。

In this example, we are using the reversed() method to reverse an array in Python.

import array as arr

# creating an array
numericArray = arr.array('i', [12, 10, 14, 16, 20, 18])
print("Array before reversing:", numericArray)

# reversing the array
newArray = list(reversed(numericArray))

# creating a new array from reversed list
revArray = arr.array('i', newArray)
print ("Array after reversing:",revArray)

执行以上代码,将显示以下输出:

On executing the above code, it will display the following output −

Array before reversing: array('i', [12, 10, 14, 16, 20, 18])
Array after reversing: array('i', [18, 20, 16, 14, 10, 12])

Using for Loop

要使用 for loop 反转数组,我们首先反转顺序遍历原始数组的元素,然后将每个元素附加到一个新数组。

To reverse an array using a for loop, we first traverse the elements of the original array in reverse order and then append each element to a new array.

Example

以下示例演示如何在 Python 中使用 for 循环反转数组。

The following example shows how to reverse an array in Python using for loop.

import array as arr
a = arr.array('i', [10,5,15,4,6,20,9])
b = arr.array('i')
for i in range(len(a)-1, -1, -1):
   b.append(a[i])
print(a)
print(b)

它将生成以下 output

It will produce the following output

array('i', [10, 5, 15, 4, 6, 20, 9])
array('i', [9, 20, 6, 4, 15, 5, 10])