有人可以解释在此Python程序中使用*吗?

问题描述 投票:4回答:2
if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()

有人可以解释以上Python代码段中对*的使用吗?

python python-3.x dictionary
2个回答
8
投票

称为拆包。它将第一项放在name中,并将所有其他项放在称为line的列表中。

name, *line = [1, 2, 3, 4]
print(name) #1
print(line) #[2, 3, 4]

2
投票

在这种情况下,name变量保存输入的第一个元素。()。split()通过使用*line返回第一个元素由line变量保留之后的所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.