我有一个句子列表,我想每次对两个句子执行一些操作,但不想对其中的所有句子进行
。例如:
list= ["aaaaa","bbbbb","ccccc","ddddd","eeeee"]
similarity_a-d = sim("aaaaa","ddddd")
similarity_a-e = sim("aaaaa","eeeee")
similarity_b-d = sim("bbbbb","ddddd")
similarity_b-e = sim("bbbbb","eeeee")
similarity_c-d = sim("ccccc","ddddd")
similarity_c-e = sim("ccccc","eeeee")
这就是我尝试过的:
similarity={}
for i,vec_lda_topic in enumerate(vec_lda_topics)[:numOfUSs]:
for j,vec_lda_topic in enumerate(vec_lda_topics)[numOfUSs:]:
similarity["sim{0}-{1}".format(i,j)] = gensim.matutils.cossim(vec_lda_topics[i], vec_lda_topics[j])
print('similarity between docs ', i, ' and ',j,': ', similarity["sim{0}-{1}".format(i,j)])
,并收到以下错误:
TypeError: 'enumerate' object is not subscriptable
除了错误之外,也许还有更好的方法吗?
答案 0 :(得分:4)
枚举切片列表,不要切片枚举列表(在两个地方都这样做)
for i,vec_lda_topic in enumerate(vec_lda_topics[:numOfUSs]):
答案 1 :(得分:2)
您的问题是您在错误的位置下标:
similarity={}
for i, vec_lda_topic in enumerate(vec_lda_topics[:numOfUSs]):
for j, vec_lda_topic in enumerate(vec_lda_topics[numOfUSs:]):
simmilarity["sim{0}-{1}".format(i,j)] = gensim.matutils.cossim(vec_lda_topics[i], vec_lda_topics[j])
print('similarity between docs ', i, ' and ', j, ': ', simmilarity["sim{0}-{1}".format(i,j)])
我所做的就是将调用中的下标移到enumerate
。