julia - How to find the key for the min/max value of a Dict?

前端 未结 4 1764
有刺的猬
有刺的猬 2021-02-20 13:57

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         


        
4条回答
  •  伪装坚强ぢ
    2021-02-20 14:39

    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) !

提交回复
热议问题