我有一个OrderedDict
:
from collections import OrderedDict
d = OrderedDict([('r', 1), ('s', 5), ('a', 3), ('n', 7), ('y', 2)])
我想根据值范围获取字典值的索引。例如,字典 d
的索引应该是数组(或 np.array
),如下所示:
indices
array([ 0, 3, 2, 4, 1 ], dtype=int64)
索引4
是字典中的最大值,索引0
是最小值
我试过了:
indices = np.argsort(d.values)
最佳答案
您可以使用列表理解来实现这一点:
>>> values = d.values()
>>> sorted_values = sorted(values)
>>> [sorted_values.index(item) for item in values]
[0, 3, 2, 4, 1]
关于python - 从字典中获取值索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047692/