How can I use two keys in Python builtin min
/max
functions?
For example, I have a list
of dict
s (they serve as counters) like this:
[{88: 3, 68: 0, 6: 0}, {88: 2, 68: 1, 6: 0}, {88: 3, 68: 0, 6: 1},
{88: 2, 68: 1, 6: 1}, {88: 3, 68: 0, 6: 2}, {88: 2, 68: 1, 6: 2},
{88: 2, 68: 0, 6: 3}, {88: 2, 68: 1, 6: 0}, {88: 1, 68: 2, 6: 0},
{88: 2, 68: 1, 6: 1}]
and I want to know which counter has the minimum cost:
cost = lambda d: sum(k * v for k, v in d.items())
as well as the minimum count:
count = lambda d: sum(d.values())
I know how to do it in normal code. I just wonder if there is any pythonic way to do this.