Python 有人能帮我把这个函数写在一行吗?

Python 有人能帮我把这个函数写在一行吗?,python,Python,这是我的解决办法。它不优雅。请帮忙 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

这是我的解决办法。它不优雅。请帮忙

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

但是为什么呢?

你为什么需要一行呢?函数有什么问题?为什么?!这个函数是很好的。不要认为“一行程序”可以产生更优雅的代码。我在这里感到很难过,因为我100%支持不必要地打高尔夫的想法。。但在这种情况下,如果代码正在做我认为它正在做的事情,我认为它不好,因为它比它需要的复杂得多(注意:不长)。当这个问题结束时,我正忙着写一个答案:这是真的一行:
result=lambda x:sum([len(v)表示k,v表示x.items()])
。你可以这样使用:
result(一些dict)
我正在学习麻省理工6.00.1x,有人说他/她可以在一行中完成。但我不相信。这就是为什么。谢谢你,伙计@从技术上讲,python中的VatoLiu一行程序是图灵完整的:您可以做任何事情。
def calculateHandlen(hand):
    return sum(v for v in hand.values() if v > 0)