根据python中字典的分组删除键/值对

时间:2019-04-28 23:36:06

标签: python json dictionary grouping key-value

我有一个JSON文件A.json,其中包含多个字典。我想从键“模型” grouped by brand中删除常见的键值对。

例如,考虑以下品牌:“福特”:

{"Number": '123', "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang2":"3.00", "Mustang3":"1.00", "Mustang4":"1.64"}}

{"Number": '891', "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang8":"3.00", "Mustang3":"1.00", "Mustang6":"1.64"}}

两个字典中共有的键model中的键是Mustang1Mustang3。因此,我从模型中删除了两个键值对。 最终字典为:

 {"Number": '123', "brand": "Ford", "model":{"Mustang2":"3.00", "Mustang4":"1.64"}}
{"Number": '891', "brand": "Ford", "model":{"Mustang8":"3.00", "Mustang6":"1.64"}}

A.json

{"Number": '123', "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang2":"3.00", "Mustang3":"1.00", "Mustang4":"1.64"}}
{"Number": '321', "brand": "Toyota", "model":{"Camry":"2.64", "Prius":"3.00", "Corolla":"1.00", "Tundra":"1.64"}}
{"Number": '111', "brand": "Honda", "model":{"Accord":"2.64", "Civic":"3.00", "Insight":"1.00", "Pilot":"1.64"}}
{"Number": '891', "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang8":"3.00", "Mustang3":"1.00", "Mustang6":"1.64"}}
{"Number": '745', "brand": "Toyota", "model":{"Camry":"2.64", "Sienna":"3.00", "4Runner":"1.00", "Prius":"1.64"}}
{"Number": '325', "brand": "Honda", "model":{"Accord":"2.64", "Passport":"3.00", "HR-V":"1.00", "Pilot":"1.64"}}
{"Number": '745', "brand": "Accura", "model":{"TLX":"2.64", "MDX":"3.00"}}
{"Number": '325', "brand": "Accura", "model":{"TLX":"2.64", "MDX":"3.00"}}

预期结果: Result.json

{"Number": '123', "brand": "Ford", "model":{"Mustang2":"3.00", "Mustang4":"1.64"}}
{"Number": '321', "brand": "Toyota", "model":{"Corolla":"1.00", "Tundra":"1.64"}}
{"Number": '111', "brand": "Honda", "model":{"Civic":"3.00", "Insight":"1.00", "Pilot":"1.64"}}
{"Number": '891', "brand": "Ford", "model":{"Mustang8":"3.00", "Mustang6":"1.64"}}
{"Number": '745', "brand": "Toyota", "model":{"Sienna":"3.00", "4Runner":"1.00"}}
{"Number": '325', "brand": "Honda", "model":{"Passport":"3.00", "HR-V":"1.00", "Civic Type R":"1.64"}}
{"Number": '745', "brand": "Accura", "model":{}}
{"Number": '325', "brand": "Accura", "model":{}}

3 个答案:

答案 0 :(得分:1)

首先,您的A.json不是常规的json文件。这是更正的版本:

[{"Number": "123", "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang2":"3.00", "Mustang3":"1.00", "Mustang4":"1.64"}},
{"Number": "321", "brand": "Toyota", "model":{"Camry":"2.64", "Prius":"3.00", "Corolla":"1.00", "Tundra":"1.64"}},
{"Number": "111", "brand": "Honda", "model":{"Accord":"2.64", "Civic":"3.00", "Insight":"1.00", "Pilot":"1.64"}},
{"Number": "891", "brand": "Ford", "model":{"Mustang1":"2.64", "Mustang8":"3.00", "Mustang3":"1.00", "Mustang6":"1.64"}},
{"Number": "745", "brand": "Toyota", "model":{"Camry":"2.64", "Sienna":"3.00", "4Runner":"1.00", "Prius":"1.64"}},
{"Number": "325", "brand": "Honda", "model":{"Accord":"2.64", "Passport":"3.00", "HR-V":"1.00", "Pilot":"1.64"}},
{"Number": "745", "brand": "Accura", "model":{"TLX":"2.64", "MDX":"3.00"}},
{"Number": "325", "brand": "Accura", "model":{"TLX":"2.64", "MDX":"3.00"}}]

文件内容应使用json模块进行解析:

import io # to test without a file
f = io.StringIO(json_text) # json_text is a string containing the text above

import json
ds = json.load(f)

第二,您必须按品牌建立set通用模型:

common_by_brand = {}
for d in ds:
    if d["brand"] in common_by_brand:
        common_by_brand[d["brand"]] &= set(d["model"])
    else:
        common_by_brand[d["brand"]] = set(d["model"])
    # {'Ford': {'Mustang1', 'Mustang3'}, 'Toyota': {'Camry', 'Prius'}, 'Honda': {'Accord', 'Pilot'}, 'Accura': {'TLX', 'MDX'}}

第三,只需遍历列表并删除那些常见模型:

for d in ds:
    common = common_by_brand[d["brand"]]
    d["model"] = {k: v for k, v in d["model"].items() if k not in common}
# [{'Number': '123', 'brand': 'Ford', 'model': {'Mustang2': '3.00', 'Mustang4': '1.64'}}, {'Number': '321', 'brand': 'Toyota', 'model': {'Corolla': '1.00', 'Tundra': '1.64'}}, {'Number': '111', 'brand': 'Honda', 'model': {'Civic': '3.00', 'Insight': '1.00'}}, {'Number': '891', 'brand': 'Ford', 'model': {'Mustang8': '3.00', 'Mustang6': '1.64'}}, {'Number': '745', 'brand': 'Toyota', 'model': {'Sienna': '3.00', '4Runner': '1.00'}}, {'Number': '325', 'brand': 'Honda', 'model': {'Passport': '3.00', 'HR-V': '1.00'}}, {'Number': '745', 'brand': 'Accura', 'model': {}}, {'Number': '325', 'brand': 'Accura', 'model': {}}]

第四,将结果以json格式写入文件:

g = io.StringIO()
json.dump(ds, g)
print (g.getvalue())

格式化输出:

[{"Number": "123", "brand": "Ford", "model": {"Mustang2": "3.00", "Mustang4": "1.64"}},
{"Number": "321", "brand": "Toyota", "model": {"Corolla": "1.00", "Tundra": "1.64"}},
{"Number": "111", "brand": "Honda", "model": {"Civic": "3.00", "Insight": "1.00"}},
{"Number": "891", "brand": "Ford", "model": {"Mustang8": "3.00", "Mustang6": "1.64"}},
{"Number": "745", "brand": "Toyota", "model": {"Sienna": "3.00", "4Runner": "1.00"}},
{"Number": "325", "brand": "Honda", "model": {"Passport": "3.00", "HR-V": "1.00"}},
{"Number": "745", "brand": "Accura", "model": {}},
{"Number": "325", "brand": "Accura", "model": {}}]

答案 1 :(得分:0)

首先,您需要使用json builtin library在python中加载json。

然后,有几种方法可以实现此目的。例如,您可以遍历每个字典并在每次迭代时更新Counter。然后,您删除已被多次计数的每个键。

最后,您再次使用json lib将结果字典转储到新文件中。

答案 2 :(得分:0)

我假设您将使用标准JSON格式。您需要检查字典中typevalue的{​​{1}}的类型为keydict方法可用于此目的。您可以使用以下代码段:

isinstance()

我希望这可能有用。 干杯:)