1

I got two different test with different test scores. I store both test in a separate list. How can I merge both list and get the final result?

returnn = sorted(returnscore, key=itemgetter('score'), reverse=True) 
for rij in returnn:
    print rij

Output:

{'E-mail': '[email protected]', 'score': 10}
{'E-mail': '[email protected]', 'score': 10}
{'E-mail': '[email protected]', 'score': 10}
{'E-mail': '[email protected]', 'score': 10}
{'E-mail': '[email protected]', 'score': 5}
{'E-mail': 'noreply@com', 'score': 5}
{'E-mail': 'marketing@nl', 'score': 5}

spff = sorted(spfscore, key=itemgetter('score'), reverse=True) 
for rij in spff:
    print rij

Output:

{'E-mail': '[email protected]', 'score': 3}
{'E-mail': '[email protected]', 'score': 0}
{'E-mail': '[email protected]', 'score': 7}
{'E-mail': '[email protected]', 'score': 0}
{'E-mail': '[email protected]', 'score': 0}
{'E-mail': 'noreply@com', 'score': 0}
{'E-mail': 'arketing@nl', 'score': 1}

The output I want is:

{'E-mail': '[email protected]', 'score': 50}
{'E-mail': '[email protected]', 'score': 5}
{'E-mail': 'noreply@com', 'score': 5}
{'E-mail': 'arketing@nl', 'score': 6}

I been trying to figure this out for a couple of hours. I just don`t understand how I can count the scores and remove the duplicates after.

1
  • How did you approach this? Try to do it yourself, you can do it ;) First merge two lists, then you can iterate and accumulate results. After the basic scenario you can check also this documentation page: docs.python.org/2/library/itertools.html Commented Oct 9, 2017 at 11:54

2 Answers 2

2

An easy way to approach this would be to simply create a new dict object which contains the E-Mail Address as its (unique) key, and then iterating through the list, incrementing the scores count if the element is already in the list, or creating the dict entry with the scores count if the E-Mail Address is not yet in the dict.

scores_raw = [{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 5},
{'E-mail': 'noreply@com', 'score': 5},
{'E-mail': 'marketing@nl', 'score': 5}]

scores_unique = {}
for item in scores_raw:
    if item['E-mail'] not in scores_unique:
        scores_unique.update({item['E-mail']: item['score']})
    else:
        scores_unique[item['E-mail']] += item['score']

print (scores_unique)

Output: {'[email protected]': 40, '[email protected]': 5, 'noreply@com': 5, 'marketing@nl': 5}

Sign up to request clarification or add additional context in comments.

Comments

1

Another way using groupby from itertools

from itertools import groupby

scores_raw = [
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 10},
{'E-mail': '[email protected]', 'score': 5},
{'E-mail': 'noreply@com', 'score': 5},
{'E-mail': 'marketing@nl', 'score': 5}
]


scores_unique=[]
for _key, _items in groupby(sorted(scores_raw),key=lambda x: x['E-mail']):    
    scores_unique.append({
        'E-mail':_key,
        'score':sum(item['score'] for item in _items)
    })

print (scores_unique)

result :

[{'E-mail': '[email protected]', 'score': 5}, {'E-mail': 'marketing@nl', 'score': 5}, {'E-mail': 'noreply@com', 'score': 5}, {'E-mail': '[email protected]', 'score': 40}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.