守着星空守着你
编写代码时的准确性和您的问题很重要。您的示例数组会产生错误:In [69]: a = np.array(10,2,3,1,20,30) ---------------------------------------------------------------------------TypeError 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]) In [71]: a Out[71]: array([10, 2, 3, 1, 20, 30])排序做你想要的:In [72]: np.sort(a) Out[72]: array([ 1, 2, 3, 10, 20, 30])但如果输入是字符串或字符串列表:In [73]: a = np.array('10,2,3,1,20,30'.split(',')) In [74]: a Out[74]: array(['10', '2', '3', '1', '20', '30'], dtype='<U2')In [75]: np.sort(a) Out[75]: array(['1', '10', '2', '20', '3', '30'], dtype='<U2')排序是字符串词法的。并且使用引号来显示字符串是很清楚的。