I have two different nested lists x and y which contain unigrams values of words from a huge corpus. Demo lists are shown below:
x = [['ali', '225'], ['raheem', '4514'], ['mohammed', '19652']]
y = [['ali', '45514'], ['mohammed', '441'], ['salwa', '41']]
As you see above, a word can be found in both x and y nested lists, but their values are different. Using Python3.3, I tried to compare the two lists and add the unigram value of a word in y to the corresponding word in x. However, I get a message error:
Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
add_unigrams(x, y)
File "C:/Python33/add_unigrams.py", line 9, in add_unigrams
x[i][0] = x[i][0], x[i][1] = int(x[i][1]) + int(y[i][1])
TypeError: 'int' object is not iterable
I would appreciate any help in getting the code work. Here is my code:
def add_unigrams(x, y):
'''(lst, lst) -> lst
Compare the items of y to items of x, add the unigrams of similar words
in y to the value of the corresponding words in x. If an item in y is not found
in x, then x.append(item).
>>> add_unigrams(x, y)
[['ali', '45739'], ['raheem', '4514'], ['mohammed', '20093'], ['salwa', '41']]
'''
i = 0
j = 0
for i in range(len(x)):
for j in range(len(y)):
if x[i][0] == y[j][0] and len(x[i]) == len(y[j]):
x[i][0] = x[i][0]
x[i][1] = int(x[i][1]) + int(y[i][1])
i = i + 1
j = j + 1
for item in x:
for item in y:
if (not item in x):
x.append(item)
return x
I thought of implementing two dictionaries to do the same task, but I don't know how to do it. Example:
d1 = { 'ali': 225, 'raheem' : 4514, 'mohammed' : 19652}
d2 = { 'ali': 45514, 'mohammed' : 441, 'salwa' : 41}
Any help is greatly appreciated!
Mohammed