【问题标题】:how to take a list of dictionary from the user?如何从用户那里获取字典列表?
【发布时间】:2022-10-07 14:43:25
【问题描述】:
l=[]
x={}
for i in range(2):
    x[\'name\']=input(\'Enter the name : \')
    print(x)
    l.append(x)
print(l)

为什么我得到这个输出?

Enter the name :  k
Enter the name :  l
[{\'name\': \'l\'}, {\'name\': \'l\'}]
  [{\'name\': \'k\'}, {\'name\': \'l\'}]
  • 你能解释一下你的期望吗?

标签: python


【解决方案1】:

每次通过循环时,您都在附加相同字典x 到列表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多