按列对 2d numpy 字符串/浮点数组进行排序

我有以下 numpy 数组(自 1957 年以来该国的火箭发射次数),我想按发射次数的升序对其进行排序。


   ['Australia', 6.0],

   ['Brazil', 3.0],

   ['China', 269.0],

   ['France', 303.0],

   ['India', 76.0],

   ['Iran', 14.0],

   ['Israel', 11.0],

   ['Japan', 126.0],

   ['Kazakhstan', 701.0],

   ['Kenya', 9.0],

   ['New Zealand', 13.0],

   ['North Korea', 5.0],

   ['Pacific Ocean', 36.0],

   ['Russian Federation', 1398.0],

   ['South Korea', 3.0],

   ['USA', 1351.0]

问题是, np.sort(a, axis = 0) 只对值进行排序,但国家之间没有联系,因此 ei 朝鲜发射了 269 枚火箭(可能比 5 枚更有可能)


或者,如果我执行 np.sort(a, axis = 1) 那么我会收到一条错误消息


类型错误:“float”和“str”实例之间不支持“<”


任何想法将不胜感激!


慕容3067478
浏览 127回答 2
2回答

拉风的咖菲猫

import numpy as npdata = [&nbsp; &nbsp;['Australia', 6.0],&nbsp;&nbsp; &nbsp;['Brazil', 3.0],&nbsp; &nbsp;['China', 269.0],&nbsp; &nbsp;['France', 303.0],&nbsp; &nbsp;['India', 76.0],&nbsp; &nbsp;['Iran', 14.0],&nbsp; &nbsp;['Israel', 11.0],&nbsp; &nbsp;['Japan', 126.0],&nbsp; &nbsp;['Kazakhstan', 701.0],&nbsp; &nbsp;['Kenya', 9.0],&nbsp; &nbsp;['New Zealand', 13.0],&nbsp; &nbsp;['North Korea', 5.0],&nbsp; &nbsp;['Pacific Ocean', 36.0],&nbsp; &nbsp;['Russian Federation', 1398.0],&nbsp; &nbsp;['South Korea', 3.0],&nbsp; &nbsp;['USA', 1351.0]]我们可以创建一个结构化数组,然后按键对其进行排序:dtype = [&nbsp; &nbsp; ('name', '<U18'),&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ('rockets', float)]data = np.array([tuple(x) for x in data], dtype=dtype)&nbsp;sorted_data = np.sort(data, order=['rockets'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(sorted_data)

侃侃尔雅

使用 python 列表排序很容易:In [208]: alist = [&nbsp; &nbsp;['Australia', 6.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Brazil', 3.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['China', 269.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['France', 303.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['India', 76.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Iran', 14.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Israel', 11.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Japan', 126.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Kazakhstan', 701.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Kenya', 9.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['New Zealand', 13.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['North Korea', 5.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Pacific Ocean', 36.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['Russian Federation', 1398.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['South Korea', 3.0],&nbsp; &nbsp; &nbsp;...:&nbsp; &nbsp; ['USA', 1351.0]]In [209]: newlist = sorted(alist, key=lambda x: x[1])In [210]: newlistOut[210]:&nbsp;[['Brazil', 3.0],&nbsp;['South Korea', 3.0],&nbsp;['North Korea', 5.0],&nbsp;['Australia', 6.0],&nbsp;['Kenya', 9.0],&nbsp;['Israel', 11.0],&nbsp;['New Zealand', 13.0],&nbsp;['Iran', 14.0],&nbsp;['Pacific Ocean', 36.0],&nbsp;['India', 76.0],&nbsp;['Japan', 126.0],&nbsp;['China', 269.0],&nbsp;['France', 303.0],&nbsp;['Kazakhstan', 701.0],&nbsp;['USA', 1351.0],&nbsp;['Russian Federation', 1398.0]]使用对象 dtype 数组(保留字符串和浮点列):In [211]: arr = np.array(alist, object)In [212]: arrOut[212]:&nbsp;array([['Australia', 6.0],&nbsp; &nbsp; &nbsp; &nbsp;['Brazil', 3.0],&nbsp; &nbsp; &nbsp; &nbsp;['China', 269.0],&nbsp; &nbsp; &nbsp; &nbsp;['France', 303.0],&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp;['USA', 1351.0]], dtype=object)只需查看第二列即可获取排序索引:In [213]: idx = np.argsort(arr[:,1])In [214]: idxOut[214]: array([ 1, 14, 11,&nbsp; 0,&nbsp; 9,&nbsp; 6, 10,&nbsp; 5, 12,&nbsp; 4,&nbsp; 7,&nbsp; 2,&nbsp; 3,&nbsp; 8, 15, 13])In [215]: arr[idx]Out[215]:&nbsp;array([['Brazil', 3.0],&nbsp; &nbsp; &nbsp; &nbsp;['South Korea', 3.0],&nbsp; &nbsp; &nbsp; &nbsp;['North Korea', 5.0],&nbsp; &nbsp; &nbsp; &nbsp;['Australia', 6.0],&nbsp; &nbsp; &nbsp; &nbsp;['Kenya', 9.0],&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp;['Russian Federation', 1398.0]], dtype=object)其他答案中的结构化数组方法也很好。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python