Hi Tutors,<br>I have two pickled dictionaries containing word counts from two different corpora. I need to add the values, so that a word count is the sum of both. If the word "man" has a count of 2 in corpus A and a count of 3 in corpus B, then I need a new dictionary that has "man": 5. Please let me know whether the following is correct/incorrect, good/bad, etc.<br>
Your help appreciated:<br><br>def addDicts(a, b):<br> c = {}<br> for k in a:<br> if k not in b:<br> c[k] = a[k]<br> else:<br> c[k] = a[k] + b[k]<br> <br> for k in b:<br> if k not in a:<br>
c[k] = b[k]<br> return c<br> <br># test this<br>dict1 = {"dad": 3, "man": 2}<br>dict2 = {"dad": 5, "woman": 10}<br>newDict = addDicts(dict1, dict2)<br>print(newDict)<br>
# This gives<br><br>{'dad': 8, 'woman': 10, 'man': 2}<br><br><br><br clear="all"><br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>"No victim has ever been more repressed and alienated than the truth"<br>
<br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br><br>--------------------------------------------------------<br>