我有两个不同的嵌套列表x和y,其中包含来自巨大语料库的单词的unigram值。演示列表如下所示:
x = [['ali', '225'], ['raheem', '4514'], ['mohammed', '19652']]
y = [['ali', '45514'], ['mohammed', '441'], ['salwa', '41']]
如上所示,在x和y嵌套列表中都可以找到一个单词,但是它们的值不同。使用Python3.3,我尝试比较两个列表,并将y中一个单词的unigram值添加到x中相应的单词。但是,我收到一条消息错误:
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
我将不胜感激代码的工作。这是我的代码:
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
我考虑过实现两个字典来完成相同的任务,但是我不知道该怎么做。例:
d1 = { 'ali': 225, 'raheem' : 4514, 'mohammed' : 19652}
d2 = { 'ali': 45514, 'mohammed' : 441, 'salwa' : 41}
任何帮助是极大的赞赏!穆罕默德
IIUC,而不是使用列表,而是使用一个collections.Counter
对象。例如:
>>> x = [['ali', '225'], ['raheem', '4514'], ['mohammed', '19652']]
>>> y = [['ali', '45514'], ['mohammed', '441'], ['salwa', '41']]
>>> x = [[k, int(v)] for k,v in x]
>>> y = [[k, int(v)] for k,v in y]
>>> Counter(dict(x))
Counter({'mohammed': 19652, 'raheem': 4514, 'ali': 225})
>>> Counter(dict(x)) + Counter(dict(y))
Counter({'ali': 45739, 'mohammed': 20093, 'raheem': 4514, 'salwa': 41})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句