Remove key-value pair based on grouping from dicti

2019-07-26 17:56发布

问题:

I have a JSON file A.json which contains multiple dictionaries. And I want to remove common key-value pairs from key "model" grouped by brand.

For example, consider the brand: "Ford":

{"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"}}

Keys in key model which are common across both the dictionaries are Mustang1 and Mustang3. So I removed both of the key-value pair from model. Final dictionaries would be:

 {"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"}}

Expected Result: 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":{}}

回答1:

First, your A.json is not a regular json file. Here's the corrected version:

[{"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"}}]

The content of the file should be parsed with the json module:

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)

Second, you have to build a set of common models by brand:

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'}}

Third, just iterate over the list and remove those common models:

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': {}}]

Fourth, write the result in json format to a file:

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

Formatted output :

[{"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": {}}]


回答2:

First, you need to load the json in python with the json builtin library.

Then, there are a few ways to achieve this. For example, you could iterate over each dict and update a Counter at each iteration. Then you remove every key which has been counted more than once.

Finally, you use the json lib again to dump the resulting dict in a new file.



回答3:

I am assuming you will be using the standard JSON format. You need to check when the type of value for a key inside a dictionary is of type dict. isinstance() method can be used for this purpose. You can use the following code snippet:

for key,value in your_json.items():
    if isinstance(value, dict):
       your_json[key]={}

I hope this might work. Cheers :)