Home > Blockchain >  Comparing key-value pairs in Python
Comparing key-value pairs in Python

Time:04-15

I have something like this

a = [{'name': 'John', 'supporterId': 1}, {'name': 'Paul', 'supporterId': 2}]
b = [{'dependent': 'Erick','supporterId': 2}, {'dependent': 'Anna', 'supporterId': 2}, {'dependent': 'George','supporterId': 13}]

and I need to check if the supporterId between a and b are equal and if so put the name_dependent inside the corresponding supporterId in a so for example the output to this should be:

c = [{'name': 'John', 'supporterId': 1}, {'name': 'Paul', 'supporterId': 2, 'dependent': [Erick, Anna]}]

I have tried many for loops inside another but it doesn't seem to work...

CodePudding user response:

The following should work:

from collections import defaultdict

a = [{'name': 'John', 'supporterId': 1}, {'name': 'Paul', 'supporterId': 2}]
b = [{'dependent': 'Erick','supporterId': 2}, {'dependent': 'Anna', 'supporterId': 2}, {'dependent': 'George','supporterId': 13}]

dependents = defaultdict(list)
for x in b:
    dependents[x['supporterId']].append(x['dependent'])

def gen_c_item(x):
    if x['supporterId'] in dependents:
        return {**x, 'dependent': dependents[x['supporterId']]}
    return x

c = [gen_c_item(x) for x in a]

This results in c being:

{'name': 'John', 'supporterId': 1}, {'name': 'Paul', 'supporterId': 2, 'dependent': ['Erick', 'Anna']}]

It builds a dict that maps from each supporter ID in b to a list of the names of the dependents, then uses that dict to construct c efficiently.

CodePudding user response:

We can do the following steps to achieve the desired result:

a = [{'name': 'John', 'supporterId': 1}, {'name': 'Paul', 'supporterId': 2}]
b = [{'dependent': 'Erick', 'supporterId': 2}, {'dependent': 'Anna', 'supporterId': 2},
     {'dependent': 'George', 'supporterId': 13}]

# Create a dictionary based on suppoertedId as key and whole object as value
by_id = {supporter['supporterId']: supporter for supporter in a}

# Iterate over b
for dependent in b:
    # Check if the dictionary has a object for the current supporterId from b
    if dependent['supporterId'] in by_id:
        # Update the depended list
        by_id[dependent['supporterId']].setdefault('dependent', []).append(dependent['dependent'])

# Get the values from the dictionary
c = list(by_id.values())

CodePudding user response:

Ok so I got this, not the cleanest solution, but gets the job done

Blockquote def pair(supporters, dependents):

pairs = []
dependentsToPair = {}

for supporter in supporters:
    dependentsToPair[supporter['supporterId']] = []
    for dependent in dependents:
        if supporter['supporterId'] == dependent['supporterId']:
            dependentsToPair[supporter['supporterId']].append(dependent)

    if dependentsToPair[supporter['supporterId']] != []:
        supporter['dependents'] = dependentsToPair[supporter['supporterId']]
    pairs.append(supporter)

return pairs

the whole thing is a function but I can't get it to show here

  • Related