在本文中,我们将向您展示如何在 python 中按升序和降序按第 n 列对 NumPy 中的数组进行排序。
NumPy是一个Python库,旨在有效地处理Python中的数组。它快速、简单易学且存储高效。它还改进了流程处理数据的方式。在 NumPy 中,我们可以生成一个 n 维数组。要使用 NumPy,我们只需将其导入我们的程序,然后我们可以轻松地在我们的代码中使用 NumPy 的功能。
方法 1.按第一列对 Numpy 数组进行排序
在这种方法中,我们使用随机模块生成一个随机的 NumPy 数组,并按升序对数组的第一列进行排序(默认)。
算法(步骤)
以下是执行所需任务要遵循的算法/步骤 -
- 使用 import 关键字导入具有别名 (np) 的 numpy 模块。
- 使用 random.randint() 方法创建一个形状为 6x6 的 NumPy 数组,其中包含 0 到 19 之间的任何随机数(返回指定范围内的随机数)。
- 打印输入数组。
- 使用 argsort() 函数(使用 kind 关键字提供的算法沿指定轴执行间接排序。它返回对数组进行排序的索引),以按第一列对输入数组进行排序,并按第一列的升序打印结果排序数组。
例
以下程序使用 argsort() 函数按第一列升序对 NumPy 数组进行排序并返回它 -
import numpy
as np
# creating a NumPy array of any random numbers from 0 to 19 of shape 6*6 inputArray
= np
.random
.randint
(
0
,
20
,
(
5
,
5
)
)
# printing the input array
(
"The input random array is:"
)
(inputArray
)
# using argsort() function, to sort the input array by the 1st column
(
"Sorting the input array by the 1st column:"
)
# Here in [:,1], ':' specifies all the rows and 1 specifies we need to sort by 1st column
(inputArray
[inputArray
[
:
,
1
]
.argsort
(
)
]
)
输出
在执行时,上述程序将生成以下输出 -
<span style="color:#000000">The input random array is:
[[ 8 16 7 5 13]
[ 5 8 6 0 2]
[11 7 3 3 6]
[ 5 3 15 7 5]
[15 3 9 14 3]]
Sorting the input array by the 1st column:
[[ 5 3 15 7 5]
[15 3 9 14 3]
[11 7 3 3 6]
[ 5 8 6 0 2]
[ 8 16 7 5 13]]
</span>
方法 2.按第 n 列对 Numpy 数组进行排序
在这种方法中,我们使用随机模块生成一个随机的 NumPy 数组,并按升序对数组的给定第 n 列进行排序(默认)。
算法(步骤)
以下是执行所需任务要遵循的算法/步骤 -
- 使用随机创建一个形状为 4x4 的 NumPy 数组,其中包含 0 到 99 之间的任何随机数。randint() 方法并打印输入数组。
- 输入 n 值并创建一个变量来存储它。
- 使用 argsort() 函数按第 n 列对输入数组进行排序,并按第一列的升序打印结果排序数组。
例
以下程序使用 argsort() 函数按给定的第 n 列升序对 NumPy 数组进行排序并返回它 -
import numpy
as np
# creating a NumPy array of any random numbers from 0 to 100 of shape 4x4 inputArray
= np
.random
.randint
(
0
,
100
,
(
4
,
4
)
)
# printing the input array
(
"The input random array is:"
)
(inputArray
)
# entering the value of n n
=
2
# using argsort() function, to sort the input array by the nth column
# here we are sorting the nth column of the array
(
"Sorting the input array by the"
,n
,
"column:"
)
# Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column
(inputArray
[inputArray
[
:
,n
]
.argsort
(
)
]
)
输出
在执行时,上述程序将生成以下输出 -
<span style="color:#000000">The input random array is:
[[32 5 68 67]
[ 7 7 12 63]
[49 49 10 15]
[96 5 93 29]]
Sorting the input array by the 2 column:
[[49 49 10 15]
[ 7 7 12 63]
[32 5 68 67]
[96 5 93 29]]
</span>
方法 3.按第 n 列以相反的顺序对 Numpy 数组进行排序
在这种方法中,我们使用随机模块生成一个随机 NumPy 数组,并通过以相反的顺序切片来降序对数组的给定第 n 列进行排序。
例
以下程序使用 argsort() 函数按给定的第 n 列降序对 NumPy 数组进行排序,并返回它 -
import numpy
as np
# creating a numpy array of any random numbers from 0 to 100 of shape 5*5 inputArray
= np
.random
.randint
(
0
,
100
,
(
5
,
5
)
)
# printing the input array
(
"The input random array is:"
)
(inputArray
)
# entering the value of n n
=
3
(
"Sorting the input array by the"
,n
,
"column:"
)
# Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column
# [::-1] slices the result in reverse order(descending order)
(inputArray
[inputArray
[
:
,n
]
.argsort
(
)
[
:
:
-
1
]
]
)
输出
在执行时,上述程序将生成以下输出 -
<span style="color:#000000">The input random array is:
[[47 6 62 53 50]
[ 5 30 13 10 33]
[43 93 57 91 91]
[84 40 21 55 9]
[76 89 85 3 4]]
Sorting the input array by the 3 column:
[[43 93 57 91 91]
[84 40 21 55 9]
[47 6 62 53 50]
[ 5 30 13 10 33]
[76 89 85 3 4]]
</span>
结论
在本文中,我们学习了如何按第 n 列对 Numpy 中的数组进行排序,以及如何使用切片按降序对其进行排序。
标签:Python,random,数组,input,array,排序,NumPy,列对 From: https://blog.51cto.com/10zhancom/6022786