【问题标题】:Not getting Expected O/P a list of dictonary or a dict of dict没有得到预期的 O/P 字典列表或 dict 的字典
【发布时间】:2019-10-12 12:28:34
【问题描述】:
d1 = {}
l1=[]
for i in range(1,3):
    d1["t"] = i
    print(d1)
    l1.append(d1)
    print("")
print("------------------------------------------------")
print("Final list: " + str(l1))

预期 最终名单:[{'t': 1}, {'t': 2}]

实际最终名单:[{'t': 2}, {'t': 2}]

【问题讨论】:

    标签: list dictionary python-3.7


    【解决方案1】:

    由于 python 中的 dict 是通过引用,因此问题已通过在循环中声明 dict 来解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多