需要有关python代码中突出显示的行的帮助:
n = int(input())
student_marks = {}
for _ in range(n):
    name, *line = input().split() <--- doubt
    scores = list(map(float, line)) <--- doubt
    student_marks[name] = scores
print (student_marks)我得到的输出如下:
2
abc 23 34 45
def 45 46 47
{'abc': [23.0, 34.0, 45.0], 'def': [45.0, 46.0, 47.0]}你们能帮我解释一下代码中标记行的必要性吗?我不能完全理解这个概念。
发布于 2018-07-17 14:45:32
name, *line = input().split() <--- doubt
input()  # reads single line of input
# 'abc 23 34 45'
.split()  # splits it into a list of whitespace separated tokens
# ['abc', '23', '34', '45']
name, *line = ...  # name is assigned first token, line a list of the remaining tokens
name  # 'abc'
line  #  ['23', '34', '45']
scores = list(map(float, line))  # maps float function onto the list of strings
scores  # [23.0, 34.0, 45.0]一些参考资料:
发布于 2021-01-05 13:13:48
1)名称,*行=输入().split()<-怀疑
*用于存储split语句的额外返回值。假设你有:
名称,*行=输入().split()
打印(名称)
打印(*行)
然后运行以下代码并输入:"abc 23 34 45",它将打印出来:
abc
23、34、45
2)分数=list(map(浮动,行))<-怀疑
在这里,map()函数在将给定函数应用于给定可迭代对象(列表、元组等)的每一项之后,返回结果的map对象(即迭代器)。有关更多了解,请参阅:https://www.geeksforgeeks.org/python-map-function/
->map()函数将浮点函数映射到字符串列表。
例如: line = 23,34,45,那么scores = list(map(float,line))将输出到: scores = 23.0,34.0,45.0
发布于 2018-07-17 14:45:10
name, *line = input().split()行用空格分隔输入,将第一个项分配到name中,其余项作为列表分配给line,在下一行scores = list(map(float, line))中,它被映射到scores中的一个浮点数列表。
https://stackoverflow.com/questions/51374688
复制相似问题