Python将项目添加到作为字典的字典值

bel*_*r01 1 python dictionary

我有一本叫做组的字典。它看起来像这样: groups = {'yellow': {}}。如何将输入名称添加到“黄色”键?
我的代码要求输入玩家的姓名:name = input("player's name")。如果一个球员的名字是约翰,我该如何采取输入的值,并将其存储这样的:groups = {'yellow': {'john': []}

Kul*_*dhu 6

If you try to set a value to a key that doesn't exist in the dictionary, it will add that to the dictionary

eg: dict[newKey] = someValue will add newKey with someValue to dict

If the key already exists it will update the value

groups = {'yellow': {}}
name = input("player's name")
groups['yellow'][name]=[]
print(groups)
Run Code Online (Sandbox Code Playgroud)
player's nameJohn
{'yellow': {'John': []}}
Run Code Online (Sandbox Code Playgroud)