如何使用 NumPy 在 Python 中对 str 进行排序

a = np.array(10,2,3,1,20,30) #I got it from the Input.
b = np.sort(a)

它给了我一个结果[1,10,2,20,3,30]而不是[1,2,3,10,20,30]


千万里不及你
浏览 158回答 3
3回答

慕容3067478

由于您正在获取输入并将其添加到数组中,因此“数字”将是str而不是ints。您需要首先将输入转换为接受int而不是str将输入更改为int:user_in&nbsp;=&nbsp;int(input())&nbsp;#&nbsp;ex:&nbsp;'4' print(type(user_in))&nbsp;#&nbsp;<class&nbsp;'int'>请注意我如何使用将int()用户input()输入从 a 转换str为 an 的方法int。现在可以将输入添加到数组中,由于值是整数而不是字符串,您将获得预期的结果。将列表中现有的项目修改为int:另一种方法是将包含数字字符串的数组更改为ints.&nbsp;您可以map()遍历数组中的所有项目,将它们更改为int.arr&nbsp;=&nbsp;list(map(int,&nbsp;arr))这将遍历您的列表并将每个项目更改为类型int

守着星空守着你

编写代码时的准确性和您的问题很重要。您的示例数组会产生错误:In [69]: a = np.array(10,2,3,1,20,30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;---------------------------------------------------------------------------TypeError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Traceback (most recent call last)<ipython-input-69-d65567166292> in <module>----> 1 a = np.array(10,2,3,1,20,30)TypeError: array() takes from 1 to 2 positional arguments but 6 were given将其更改为整数列表:In [70]: a = np.array([10,2,3,1,20,30])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [71]: a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[71]: array([10,&nbsp; 2,&nbsp; 3,&nbsp; 1, 20, 30])排序做你想要的:In [72]: np.sort(a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[72]: array([ 1,&nbsp; 2,&nbsp; 3, 10, 20, 30])但如果输入是字符串或字符串列表:In [73]: a = np.array('10,2,3,1,20,30'.split(','))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [74]: a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[74]: array(['10', '2', '3', '1', '20', '30'], dtype='<U2')In [75]: np.sort(a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[75]: array(['1', '10', '2', '20', '3', '30'], dtype='<U2')排序是字符串词法的。并且使用引号来显示字符串是很清楚的。

BIG阳

import numpy as nparr = np.array([10,2,3,1,20,30])print(np.sort(arr))这很好用。首先创建一个列表,然后再尝试对其进行排序。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python