【发布时间】:2019-11-30 14:31:43
【问题描述】:
我一直在研究如何处理字典以及如何阅读它们的实验室。我遇到了一个问题,它让我们要求用户输入将其添加到字典中。
我被卡住的原因是因为我已经枚举了键的输入,我不知道如何在不担心键的情况下向字典中添加更多内容。我是否必须退后一步,重新设计密钥的创建方式?
dInput = input('Please enter a string ')
D = dict(enumerate(dInput))
print(D)
## This is where I enumerate the user input
plusDict = input('Enter another character to add to the dictionary ')
## User puts in a character and the script adds it to the dictionary with it's proper key.
举个例子:
Please enter a string: fff
{0: 'f', 1: 'f', 2: 'f'}
Enter another character to add to the dictionary: e
想要的结果:
{0: 'f', 1: 'f', 2: 'f', 3: 'e'}
我已经删除了大部分允许用户通过输入访问字典的脚本,因为这是我唯一遇到的问题。感谢您的宝贵时间!!
【问题讨论】:
-
你想
update字典吗? -
如果这正是您想要的行为,您可以使用
d[len(d)] = plusDict,但我想知道它与列表的不同之处。
标签: python python-3.x dictionary input enumeration