Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

NumPy 数组排序


数组排序

排序意味着将元素放入一个有序序列中。

有序序列是任何具有对应于元素的顺序的序列,例如数字或字母顺序,升序或降序。

NumPy 的 ndarray 对象有一个名为sort()的函数,它可以对指定的数组进行排序。

示例

对数组进行排序

import numpy as np

arr = np.array([3, 2, 0, 1])

print(np.sort(arr))
自己尝试 »

注意:此方法返回数组的副本,原始数组保持不变。

您还可以对字符串数组或任何其他数据类型的数组进行排序

示例

按字母顺序排序数组

import numpy as np

arr = np.array(['banana', 'cherry', 'apple'])

print(np.sort(arr))
自己尝试 »

示例

排序布尔数组

import numpy as np

arr = np.array([True, False, True])

print(np.sort(arr))
自己尝试 »

排序二维数组

如果对二维数组使用 sort() 方法,则两个数组都将被排序

示例

排序二维数组

import numpy as np

arr = np.array([[3, 2, 4], [5, 0, 1]])

print(np.sort(arr))
自己尝试 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.