我有一本叫做组的字典。它看起来像这样: groups = {'yellow': {}}。如何将输入名称添加到“黄色”键?
我的代码要求输入玩家的姓名:name = input("player's name")。如果一个球员的名字是约翰,我该如何采取输入的值,并将其存储这样的:groups = {'yellow': {'john': []}?
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)
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |