在这个特定场景中,Python3.7中的星号负责做什么?

2024-09-28 21:01:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在CodeWars上完成了一个Kata,看到别人的代码用了一个我从未见过的星号。我从未真正了解过星号在python中的作用,我想知道是否有人能解释它在这种特殊情况下的作用

代码的要点是根据有多少人喜欢某样东西而返回不同的消息:

def likes(names): #names is an array of names. E.g. ["Dave", "Billy", "Bob"]
    n = len(names)
    return {
        0: 'no one likes this',
        1: '{} likes this', 
        2: '{} and {} like this', 
        3: '{}, {} and {} like this', 
        4: '{}, {} and {others} others like this'
    }[min(4, n)].format(*names[:3], others=n-2)

最后一行名字前面的星号就是我要问的


Tags: and代码消息namesisdef情况星号