Python 简明教程

Python - Sort Arrays

Python’s array 模块定义了数组类。数组类的对象类似于 Java 或 C/C++ 中存在的数组。与内置 Python 序列不同,数组是 strings 、整数或浮点数对象的同构集合。

Python’s array module defines the array class. An object of array class is similar to the array as present in Java or C/C++. Unlike the built-in Python sequences, array is a homogenous collection of either strings, or integers, or float objects.

数组类没有任何函数/方法来对它的元素进行排序的排列。然而,我们可以运用下列方法之一来实现它 −

The array class doesn’t have any function/method to give a sorted arrangement of its elements. However, we can achieve it with one of the following approaches −

  1. Using a sorting algorithm

  2. Using the sort() method from List

  3. Using the built-in sorted() function

我们逐一详细讨论这些方法。

Let’s discuss each of these methods in detail.

sorting array python

Sort Arrays Using a Sorting Algorithm

我们以经典 bubble sort algorithm 方式实现它以获取排序后的数组。要做到这一点,我们用两个 nested loops ,然后交换元素以按排序顺序重新排列。

We implement the classical bubble sort algorithm to obtain the sorted array. To do it, we use two nested loops and swap the elements for rearranging in sorted order.

Example

使用 Python 代码编辑器运行以下代码 −

Run the following code using a Python code editor −

import array as arr
a = arr.array('i', [10,5,15,4,6,20,9])
for i in range(0, len(a)):
   for j in range(i+1, len(a)):
      if(a[i] > a[j]):
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
print (a)

它将生成以下 output

It will produce the following output

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

Sort Arrays Using sort() Method of List

尽管阵列模块没有 sort() method ,但 Python 的内置 List 类有一个 sort 方法。我们将在下一个示例中使用它。

Even though array module doesn’t have a sort() method, Python’s built-in List class does have a sort method. We shall use it in the next example.

首先,声明一个数组,并利用 tolist() 方法从中获取一个列表对象。然后,使用 sort() 方法获取一个排序后的列表。最后,使用排序后的列表创建另一个数组,它会显示一个排序后的数组。

First, declare an array and obtain a list object from it, using tolist() method. Then, use the sort() method to get a sorted list. Lastly, create another array using the sorted list which will display a sorted array.

Example

以下代码展示了如何使用 sort() 方法获取一个排序后的数组。

The following code shows how to get sorted array using the sort() method.

import array as arr

# creating array
orgnlArray = arr.array('i', [10,5,15,4,6,20,9])
print("Original array:", orgnlArray)
# converting to list
sortedList = orgnlArray.tolist()
# sorting the list
sortedList.sort()

# creating array from sorted list
sortedArray = arr.array('i', sortedList)
print("Array after sorting:",sortedArray)

上述代码将显示以下输出 −

The above code will display the following output −

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

Sort Arrays Using sorted() Method

第三种数组排序方法是使用 sorted() 函数,它是一个 built-in function

The third technique to sort an array is with the sorted() function, which is a built-in function.

sorted() 函数的 syntax 如下 −

The syntax of sorted() function is as follows −

sorted(iterable, reverse=False)

该函数返回一个新列表,包含所有元素按升序排序的 iterable 对象。将逆序参数设置为 True,以获取按降序排序的元素。

The function returns a new list containing all items from the iterable in ascending order. Set reverse parameter to True to get a descending order of items.

sorted() 函数可以与任何 iterable 对象一起使用。Python 数组是一个 iterable 对象,因为它是一个索引集合。因此,一个数组可用作 sorted() 函数的参数。

The sorted() function can be used along with any iterable. Python array is an iterable as it is an indexed collection. Hence, an array can be used as a parameter to sorted() function.

Example

在这个示例中,我们将看到 sorted() 方法在数组排序中的应用。

In this example, we will see the use of sorted() method in sorting an array.

import array as arr
a = arr.array('i', [4, 5, 6, 9, 10, 15, 20])
sorted(a)
print(a)

它将生成以下 output

It will produce the following output

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