简体   繁体   English

Pythonic方法从字典中查找加权最小值和最大值的关键字

[英]Pythonic way to find key of weighted minimum and maximum from a dictionary

I'm working with a dataset similar to this: 我正在使用与此类似的数据集:

animals = {
            "antelope": {
                "latin": "Hippotragus equinus", 
                "cool_factor": 1, 
                "popularity": 6
            }, 
            "ostrich": {
                "latin": "Struthio camelus", 
                "cool_factor": 3, 
                "popularity": 3
            }, 
            "echidna": {
                "latin": "Tachyglossus aculeatus", 
                "cool_factor": 5, 
                "popularity": 1
            }
          }

What I'm looking to do is find the "least cool" and "coolest" animal weighted by popularity, such that: 我想要做的是找到受欢迎程度加权的“最不酷”和“最酷”的动物,这样:

> min_cool_weighted(animals)
  "echidna"

> max_cool_weighted(animals)
  "ostrich"

The solution that comes to me first is to create 3 arrays ( keys , cool_factors , and popularities ), loop through the dictionary, push all the values into the 3 arrays, then create a fourth array with each value where weighted[i] = cool_factor[i] * popularity[i] , then take the min/max and grab the corresponding key from the key array. 首先来到我的解决方案是创建3个数组( keyscool_factorspopularities ),循环遍历字典,将所有值推入3个数组,然后创建第四个数组,每个值weighted[i] = cool_factor[i] * popularity[i] ,然后取最小值/最大值并从键阵列中获取相应的键。 However, this doesn't seem very Pythonic. 但是,这似乎不是Pythonic。

Is there a better, more expressive way? 有更好,更有表现力的方式吗?

max and min should suffice 最大最小应该足够了

min(animals, key=lambda x: animals[x]["cool_factor"]*animals[x]["popularity"])
'echidna'
max(animals, key=lambda x: animals[x]["cool_factor"]*animals[x]["popularity"])
'ostrich'

You can use sorted 你可以使用已sorted

Min: 闵:

sorted(animals.iteritems(), 
       key=lambda x:x[1]['cool_factor']*x[1]['popularity'])[0][0]

Max: 马克斯:

sorted(animals.iteritems(), 
       key=lambda x:x[1]['cool_factor']*x[1]['popularity'])[-1][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pythonic方法找到与嵌套字典相关的键 - Pythonic way to find the key associated with nested dictionary 查找与另一个字典中的键、值对匹配的字典的 Pythonic 方法 - Pythonic way to find a dictionary that matches key, value pairs in another dictionary 具有双值键的Python字典,需要查找最大值和最小值 - Python Dictionary with dual values key, need to find maximum and minimum value 查找列表的最大绝对值的Pythonic方法 - Pythonic way to find maximum absolute value of list 如何在最小和最大尺寸条件下应用熊猫组(Pythonic方式) - How to apply panda group by with minimum and maximum size condition (Pythonic way) Pythonic方法使键/值对成为字典 - Pythonic way to make a key/value pair a dictionary 如何找到字典中每个键的最小值/最大值? 以及如何找到每个键的值的数量? - How do I find the minimum/maximum value for each key in a dictionary? And how do I find the number of values for each key? 从字典列表中查找具有键最大值的唯一项 - Find unique item from list of dictionary with the maximum value of a key 以动态pythonic方式查找部分有序集中的最小元素 - Find in a dynamic pythonic way the minimum elements in a partially ordered set Pythonic从嵌套字典生成列表的方法 - Pythonic way to produce a list from nested dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM