skip to Main Content

What I’m lloking to get is :

From:

arrayList = [{
            "value": 10,
            "name": "BBB",
            "extra": "sample"
        },{
            "value": 10,
            "name": "BBB",
        }, {
            "value": 10,
            "name": "AAA",
            "extra": "sample"
        }, {
            "value": 10,
            "name": "AAA",
        }
    ]

Based on the selected nameKey and valueKey, deduplication list + sum up values

So if input values will be: nameKey = "name" and valueKey = "value"
Output will be:

arrayList = [{
            "value": 20,
            "name": "BBB",
            "extra": "sample"
        }, {
            "value": 20,
            "name": "AAA",
            "extra": "sample"
        }
    ]

And if input values will be: nameKey = "extra" and valueKey = "value"
Output will be:

arrayList = [{
            "value": 20,
            "name": ["BBB","AAA"], <- If keys are the same, them they will be added to array
            "extra": "sample"
        },{
            "value": 10,
            "name": "BBB",
        }, {
            "value": 10,
            "name": "AAA",
        }
    ]

For the simplededuplication currently I’m using this: [dict(t) for t in {tuple(d.items()) for d in arrayList }]

But not sure how to do they way I have mentioned above, as it is sounds too complicated to make it works like this

2

Answers


  1. Not too sure if this is what you was asking for but here is my assumption:

    import json
    from collections import defaultdict
    
    arrayList = [
        {
            "value": 10,
            "name": "BBB",
            "extra": "sample"
        },
        {
            "value": 10,
            "name": "BBB",
        },
        {
            "value": 10,
            "name": "AAA",
            "extra": "sample"
        },
        {
            "value": 10,
            "name": "AAA",
        }
    ]
    
    nameKey = "extra"
    valueKey = "value"
    
    result = defaultdict(lambda: {"name": [], valueKey: 0})
    
    for item in arrayList:
        keyValue = item.get(nameKey)
        if isinstance(keyValue, list):
            for key in keyValue:
                result[key]["name"].append(item["name"])
                result[key][valueKey] += item[valueKey]
        elif keyValue is not None:
            result[keyValue]["name"].append(item["name"])
            result[keyValue][valueKey] += item[valueKey]
    
    output = []
    for key, value in result.items():
        output.append({**value, nameKey: key})
    
    x = json.dumps(output, indent=4)
    print(x)
    
    Login or Signup to reply.
  2. my approach in solving this simple, but depends on JsonDF package that I was working on, which gives you control over Json.

    I will suggest to make a Json Template and insert to it the values, every time you insert search fot the ‘key’ which you want to find dublicates with, and from the Json that have that key you can get other values and compate it, and make the summtion and update the Json Values.

    the code will be something like this :

    from JsonDF.utils.Json.Json import json
    
    temp = Json('template', {}).objectify()
    
    # make the normal loob and start to insert values in the temp
    
    temp.insert('duplicates', Json('dublicate_1', {'name': name, 'value': value, 'extra': extra}))
    
    # in every iteration
    
    rs = temp.find_all(name)
    for r in rs:
        temp.insert(name, temp.name.value + r.value)
    

    this will do the trick for you

    you can find the packge on GitHub
    or install it via pip install JsonDF

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search