猿问

如何将多个值添加到python字典中的键?

M = int(input())

mydict = {}

for i in range (M):

    j,k = map(int, input().split())

    try:

        mydict[j].append(k)

    except KeyError :

        mydict[j] = k

print(mydict)

当我运行代码时:


Traceback (most recent call last):

  File "source_file.py", line 7, in <module>

    mydict[j].append(k)

AttributeError: 'int' object has no attribute 'append'


qq_花开花谢_0
浏览 229回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答