我在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)
最后一行名字前面的星号就是我要问的
是拆包员。它使列表中的元素作为单独的参数传递
相关问题 更多 >
编程相关推荐