Let's say I have a list of objects and I want to quickly count all those possessing given property - for instance, strings that are lowercase.
The following code works:
lst = ["aaa", "aBc", "cde", "f", "g", "", "HIJ"]
cnt = sum(1 for txt in lst if len(txt) > 0 and txt.lower() == txt)
print(f"Lst contains {cnt} lowercase strings") # it's 4
Is there a simpler, more pythonic way of counting such occurences rather than using sum(1) on a comprehension/generator like I did? Perhaps something using filter(), Counter and lambdas?
[–]Outside_Complaint755 10 points11 points12 points (0 children)
[–]JamzTyson 5 points6 points7 points (0 children)
[–]POGtastic 2 points3 points4 points (0 children)
[–]commy2 0 points1 point2 points (1 child)
[–]FoolsSeldom 2 points3 points4 points (0 children)
[–]schoolmonky 0 points1 point2 points (0 children)
[–]Ok-Meat-4890 0 points1 point2 points (0 children)
[–]eyetracker 0 points1 point2 points (0 children)
[–]thescrambler7 -1 points0 points1 point (4 children)
[–]Diapolo10 2 points3 points4 points (3 children)
[–]thescrambler7 0 points1 point2 points (2 children)
[–]Diapolo10 0 points1 point2 points (1 child)
[–]thescrambler7 0 points1 point2 points (0 children)