Python遍历字典到列表中出现覆盖前面数据或者字典对值(值为列表)赋值出现重复的问题

1.遍历字典示例1

dict_list = [
    {'key': '1'},
    {'key': '9670'},
    {'key': 'Converse'},
    {'key': 'Converse All Star 70 高帮复古黑 1970s 2018新款'},
    {'key': '866248'},
    {'key': '2018'},
    {'key': '162050C'},
    {'key': '37900'}
]
def fun(dict_list):
    list_1 = [] # 接收value值
    list_2 = [] # 接收新的字典
    dict_info = {}
    for i in range(len(dict_list)):
        list_1.append(dict_list[i]["key"])
    list_1.sort() # 排序
    for i in list_1:
        dict_info["key"] = i
        print(dict_info)
        # 运行的结果是一样的
        list_2.append(dict_info)

        # 打印地址
        for i in list_2:
            print("地址:",id(i))
    return list_2

print(fun(dict_list))

运行结果:
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
地址: 2275698218760
[{'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}, {'key': '高帮复古黑 2018新款'}]

2.解决方法

因为每次添加的都是同一个内存到list中了,dict_info每次写入的时候改变了内存中的value,
但是地址是不变,即,创建了一次内存空间,只会不断的改变value.
完善方法:每次遍历时候创建一个新的dict_new.

def fun(dict_list):
  list_1 = []
  list_2 = []
  for i in range(len(dict_list)):
    list_1.append(dict_list[i]["key"])
    list_1.sort()  # 排序
  for i in list_1:
    dict_new = {}  # 每次遍历时创建一个新的内存
    dict_new["key"] = i
    list_2.append(dict_new)
  return list_2
print(fun(dict_list))

运行结果:
地址不相同
地址: 1941366120384
地址: 1941366052616
地址: 1941366052760
地址: 1941366052832
地址: 1941366052904
地址: 1941366052976
地址: 1941366053048
地址: 1941366120384
地址: 1941366120240
[{'key': '1'}, {'key': '162050C'}, {'key': '2018'}, {'key': '37900'}, {'key': '866248'}, {'key': '9670'}, {'key': 'Converse'}, {'key': '高帮复古黑 2018新款'}]

3.遍历字典示例2

 all_items = [] # 放在页码上方,会覆盖数据
 # 每页20个商品
 for page in range(0, 20, 20):
     print(page)
     for product in productList:
                      
          itemss ={}  # 每次遍历时创建一个新的内存
         
          itemss["platform"] = 1 # 淘宝 1 京东 2
          itemss["gId"] = product.get("spuId")  # 商品编号
          itemss["brand"] = brand  # 商品品牌id
          itemss["title"] = product.get("title")  # 商品名称
          itemss["soldNum"] = product.get("soldNum")   # 销量
          itemss["locations"] = 1  # 所在榜单 1:鞋,2:服装
          itemss["goodNumber"] = product.get("articleNumber")  # 商品货号
          itemss["minSalePrice"] = product.get("minSalePrice")  # 商品售价                  
       
          print("items==", itemss)
     all_items.append(itemss)

运行结果

4.解决方法


 # 每页20个商品
 for page in range(0, 20, 20):
     print(page)
     all_items = [] # 总列表套字典,要放在遍历页码下方,也不能放在页码上方,不然会覆盖数据.

     for product in productList:
                      
          itemss ={}  # 每次遍历时创建一个新的内存
         
          itemss["platform"] = 1 # 淘宝 1 京东 2
          itemss["gId"] = product.get("spuId")  # 商品编号
          itemss["brand"] = brand  # 商品品牌id
          itemss["title"] = product.get("title")  # 商品名称
          itemss["soldNum"] = product.get("soldNum")   # 销量
          itemss["locations"] = 1  # 所在榜单 1:鞋,2:服装
          itemss["goodNumber"] = product.get("articleNumber")  # 商品货号
          itemss["minSalePrice"] = product.get("minSalePrice")  # 商品售价                  
       
          print("items==", itemss)
     all_items.append(itemss)

运行结果:

posted @ 2020-12-23 14:48  莫贞俊晗  阅读(717)  评论(0编辑  收藏  举报