Python 根据特定列对numpy数组(包含字符串、字典和数字)进行排序

Python 根据特定列对numpy数组(包含字符串、字典和数字)进行排序,python,arrays,numpy,sorting,Python,Arrays,Numpy,Sorting,我有以下numpy数组(包含字符串、字典和数字),并希望根据最后一列(数字)对该数组进行排序 这可能是使用最后一列X[-1]作为排序键的一种方法 import numpy as np X=np.array([['few', {'age': 'young'}, {'salary': 'low'}, 0.8], ['few', {'salary': 'low'}, {'age': 'young'}, 0.3], ['few', {'age': 'young'}, {'salary': 'mediu

我有以下numpy数组(包含字符串、字典和数字),并希望根据最后一列(数字)对该数组进行排序


这可能是使用最后一列
X[-1]
作为排序键的一种方法

import numpy as np

X=np.array([['few', {'age': 'young'}, {'salary': 'low'}, 0.8],
['few', {'salary': 'low'}, {'age': 'young'}, 0.3],
['few', {'age': 'young'}, {'salary': 'medium'}, 0],
['most', {'salary': 'high'}, {'education': 'upper'}, 1]])

X = np.array(sorted(X, key=lambda X: X[-1]) )
# array([['few', {'age': 'young'}, {'salary': 'medium'}, 0],
#        ['few', {'salary': 'low'}, {'age': 'young'}, 0.3],
#        ['few', {'age': 'young'}, {'salary': 'low'}, 0.8],
#        ['most', {'salary': 'high'}, {'education': 'upper'}, 1]],
#       dtype=object)
另一个选项是使用
-1
表示最后一个条目/索引的位置

from operator import itemgetter

X = sorted(X, key=itemgetter(-1))

在您的解决方案中,我可以使用X[0]访问第0行,但我希望使用X[0,:]访问它,就像在原始版本中一样data@diyar:您必须将输出转换为numpy数组才能执行
X[0,:]
切片。检查我编辑的解决方案。让我知道它是否有效
from operator import itemgetter

X = sorted(X, key=itemgetter(-1))