As I mentioned in comments you can use a generator within sum()
function like following:
sum(value == 0 for value in D.values())
Or as a slightly more optimized and functional approach you can use map
function as following:
sum(map((0).__eq__, D.values()))
Benchmark:
In [56]: %timeit sum(map((0).__eq__, D.values()))
1000000 loops, best of 3: 756 ns per loop
In [57]: %timeit sum(value == 0 for value in D.values())
1000000 loops, best of 3: 977 ns per loop
Note that although using map
function in this case may be more optimized but in order to achieve a comprehensive and general idea about the two approaches you should run the benchmark for relatively large datasets as well. Then you can decide when to use which in order to gain more performance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…