I am working with a dataset like 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 for is to find the “least cool” and “coolest” animal, weighted by popularity, for example:
> 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 ), skip the dictionary, pop all the values into 3 arrays, and then create a fourth array with each value, where weighted[i] = cool_factor[i] * popularity[i] , then take min / max and take the corresponding key from the key array. However, this is not like Pythonic.
Is there a better, more expressive way?
source share