在Python字典中编写值

时间:2016-01-28 17:21:37

标签: python-3.x

我是python的新手,因此问题。

我的输入方式如下,

Krishna 67 68 69 36 42

有几行输入,第一个字符串给出了学生的名字,后跟他的标记。我需要创建一个哈希映射并将标记添加到每个学生的列表中。 此外,我需要在没有更多输入时停止。我怎么能在Python 3中做到这一点。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

grades = {}
#for each input (using some kind of for/while loop to get lines of input)
    #split the string at the spaces into a list of strings
    info = input_str.split()
    #the first string is a name 
    #if using first and last name us " ".join(info[0:2])
    name = info[0]
    #convert each score to an int() using a list comprehension
    scores = [int(score) for score in info[1:]]
    #save the result
    grades[name] = scores