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 @   莫贞俊晗  阅读(731)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示