这是我的解决方案。它不优雅。请帮忙。
def calculateHandlen(hand):
"""
Returns the length (number of letters) in the current hand.
hand: dictionary (string int)
returns: integer
"""
num = 0
keyS = hand.keys()
for key in keyS:
if hand[key] > 0:
num += hand[key]
return num
答案 0 :(得分:4)
def calculateHandlen(hand):
return sum(v for v in hand.values() if v > 0)
但为什么?