首页 > 解决方案 > 使用 for 循环对每个键取多个值

问题描述

student_marks = {'Raj':[20,30,40,48], 'Raja':[25,35,46,22]}

我想编写一个代码,使用户能够以上述形式输入。我已经看到了针对每个键获取一个值的代码片段,但找不到将列表作为值获取的代码片段。

标签: pythonlistdictionaryfor-loop

解决方案


我猜你想要这样的东西

dic = {}
while True:
    student = input("enter student name : ")
    if student:
        mark = input("enter student mark split by ',' : ").split(",")
        dic.update({str(student): mark})
    else:
        break

print(dic)


推荐阅读