我正在使用与此类似的数据集:
@H_404_13@解决方法
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 } }
我想要做的是找到受欢迎程度加权的“最不酷”和“最酷”的动物,这样:
> min_cool_weighted(animals) "echidna" > max_cool_weighted(animals) "ostrich"
首先来到我的解决方案是创建3个数组(键,cool_factors和流行度),循环遍历字典,将所有值推入3个数组,然后创建第四个数组,每个值加权[i] = cool_factor [i] * popular [i],然后取最小值/最大值并从键阵列中获取相应的键.但是,这似乎不是Pythonic.
有更好,更有表现力的方式吗?