简体   繁体   中英

python merge two different lists to one

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': 'tim@gmail.com', 'score': 10}
{'E-mail': 'tim@gmail.com', 'score': 10}
{'E-mail': 'tim@gmail.com', 'score': 10}
{'E-mail': 'tim@gmail.com', 'score': 10}
{'E-mail': 'hihallo@gmail.com', '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': 'tim@gmail.com', 'score': 3}
{'E-mail': 'tim@gmail.com', 'score': 0}
{'E-mail': 'tim@gmail.com', 'score': 7}
{'E-mail': 'tim@gmail.com', 'score': 0}
{'E-mail': 'hihallo@gmail.com', 'score': 0}
{'E-mail': 'noreply@com', 'score': 0}
{'E-mail': 'arketing@nl', 'score': 1}

The output I want is:

{'E-mail': 'tim@gmail.com', 'score': 50}
{'E-mail': 'hihallo@gmail.com', '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.

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': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'hihallo@gmail.com', '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: {'tim@gmail.com': 40, 'hihallo@gmail.com': 5, 'noreply@com': 5, 'marketing@nl': 5}

Another way using groupby from itertools

from itertools import groupby

scores_raw = [
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'tim@gmail.com', 'score': 10},
{'E-mail': 'hihallo@gmail.com', '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': 'hihallo@gmail.com', 'score': 5}, {'E-mail': 'marketing@nl', 'score': 5}, {'E-mail': 'noreply@com', 'score': 5}, {'E-mail': 'tim@gmail.com', 'score': 40}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM