Python: List of dictionary stores only last appended value in every iteration

前端 未结 4 1038
一个人的身影
一个人的身影 2021-01-13 17:41

I have this list of dictionary:

MylistOfdict = [{\'Word\': \'surveillance\',
  \'Word No\': 1},
 {\'Word\': \'equivocal\',
  \'Word No\': 2}]
4条回答
  •  盖世英雄少女心
    2021-01-13 17:57

    Don't use the same dict, make copies of them:

    word_db2 = []
    
    key = 1
    for i in MylistOfdict:
        for j in range(1, 4):
            i = dict(i)
            i['Card Type'] = 'Type '+str(j)
            i['Card Key'] = key
            print(i)
    
            word_db2.append(i)
            key += 1
    

提交回复
热议问题