julia - 如何找到Dict的最小/最大值的键? [英] julia - How to find the key for the min/max value of a Dict?
问题描述
我想找到对应于julia中字典的最小或最大值的键。在Python中,我将执行以下操作:
my_dict = {1:20,2:10}
min(my_dict ,my_dict.get)
哪个将返回密钥2。
如何在julia做同样的事情?
my_dict = Dict(1 => 20,2 => 10)
minimum(my_dict)
后者返回1 => 20 2 => 10或2。
另一个选项是:
collect(keys(d))[indmin(collect(values(d))]]
这取决于不能保证的键和值迭代器的属性,但事实上对于OrditionsDicts的Dicts(和 )是有保证的。如 reduce
答案, d
必须不为空。
为什么提到这个,当
减少
时,几乎是指甲吗?快3到4倍(至少在我的电脑上)! I want to find the key corresponding to the min or max value of a dictionary in julia. In Python I would to the following:
my_dict = {1:20, 2:10}
min(my_dict, my_dict.get)
Which would return the key 2.
How can I do the same in julia ?
my_dict = Dict(1=>20, 2=>10)
minimum(my_dict)
The latter returns 1=>20 instead of 2=>10 or 2.
another option is:
collect(keys(d))[indmin(collect(values(d)))]
it depends on properties of keys and values iterators which are not guaranteed, but in fact work for Dicts (and are guaranteed for OrderedDicts). like the reduce
answer, d
must be non-empty.
why mention this, when the reduce
, pretty much nails it? it is 3 to 4 times faster (at least on my computer) !
这篇关于julia - 如何找到Dict的最小/最大值的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!