美文网首页
Python合并字典

Python合并字典

作者: 微凉_半夏 | 来源:发表于2018-12-28 11:50 被阅读0次

    1.对于两个字典根据其中的唯一相同键值对进行合并

    这里我采用的是两个列表中的字典,并进行合并,将唯一键值相同的字典进行合并
    dict1 = [{"names": "80*65*38 天蓝色 ", "pvs": "21433:60656511;1627207:3232484", "skuId": "3212141652714"},
             {"names": "80*65*38 深卡其布色 ", "pvs": "21433:60656511;1627207:3232482", "skuId": "3212141652715"},
             {"names": "80*65*38 粉红色 ", "pvs": "21433:60656511;1627207:3232480", "skuId": "3212141652716"},
             {"names": "80*65*38 紫罗兰 ", "pvs": "21433:60656511;1627207:80882", "skuId": "3212141652717"},
             {"names": "80*65*38 红色 ", "pvs": "21433:60656511;1627207:28326", "skuId": "3212141652718"},
             {"names": "80*65*38 绿色 ", "pvs": "21433:60656511;1627207:28335", "skuId": "3212141652719"},
             {"names": "80*65*38 黄色 ", "pvs": "21433:60656511;1627207:28324", "skuId": "3212141652720"}]
    
    dict2 = [{"priceCent": 398, "price": "3.98", "stock": 49853, "skuId": "3212141652719"},
             {"priceCent": 398, "price": "5.66", "stock": 49738, "skuId": "3212141652720"},
             {"priceCent": 398, "price": "11.78", "stock": 49646, "skuId": "3212141652714"},
             {"priceCent": 398, "price": "45.23", "stock": 49351, "skuId": "3212141652718"},
             {"priceCent": 398, "price": "99", "stock": 49347, "skuId": "3212141652717"},
             {"priceCent": 398, "price": "8.88", "stock": 48503, "skuId": "3212141652715"},
             {"priceCent": 398, "price": "9", "stock": 48853, "skuId": "3212141652716"}]
    
    list1 = []
    for dict_1 in dict1:  # 遍历第一个字典
        id1 = dict_1['skuId']  # 获取其中的键值
        name = dict_1['names']
        for dict_2 in dict2:  # 遍历第二个列表
            id2 = dict_2['skuId']
            price = dict_2['price']
            if id1 == id2:  # 根据skuId唯一相同的合并
                dict3 = {'name': name, 'price': price, 'skuId': id2}
                list1.append(dict3)
    
    print(list1)
    

    我用的是淘宝商品合并,合并对应尺寸对应颜色对应价格

    2.分割一个列表中,字典键值是相同的到分别两个列表中

    list1 = [{"names": "80*65*38 红色 ", "pvs": "21433:60656511;1627207:28326", "skuId": "3212141652718"},
             {"priceCent": 398, "price": "5.66", "stock": 49738, "skuId": "3212141652720"},
             {"names": "80*65*38 绿色 ", "pvs": "21433:60656511;1627207:28335", "skuId": "3212141652719"},
             {"names": "80*65*38 黄色 ", "pvs": "21433:60656511;1627207:28324", "skuId": "3212141652720"},
             {"priceCent": 398, "price": "11.78", "stock": 49646, "skuId": "3212141652714"},
             {"priceCent": 398, "price": "45.23", "stock": 49351, "skuId": "3212141652718"}]
    name = []
    price = []
    for dict1 in list1:
        for k, v in dict1.items():
            if k == 'priceCent':  # 如果字典中的键含有priceCent,放到price列表中
                price.append(dict1)
            elif k == 'names':
                name.append(dict1)
    print(name)
    print('---------------')
    print(price)
    

    相关文章

      网友评论

          本文标题:Python合并字典

          本文链接:https://www.haomeiwen.com/subject/eyjslqtx.html