0

我需要返回列表中单词相似度的最小平均值(我可以这样做),但我不知道如何将单词字符串值分配给平均值,例如在列表['my', 'bro', 'myline']中当然 bro 会最少word比较值,因此我需要'bro'与比较值一起返回(我已经知道如何......只需要知道如何在嵌套循环中分配值)。

final_tup = ()
ave_list = []
for word in final_list:
    word1 = word
    sum = 0
    for word in final_list:
        word2 = word
        if word2 != word1:
            num = cossim(word1,word2)
            sum = sum + num
    average = float(sum/(len(final_list)-1))
    ave_list.append(average) 
4

1 回答 1

0
from operator import itemgetter

# Make the word list.
word_list = ['my', 'bro', 'myline'] 

# Do your code here to create ave_list

# Fetch the min index and min value of ave_list
min_index, minvalue = min(enumerate(ave_list), key = itemgetter(1) )

# Now get the word at the same min index location.
min_word = word_list[min_index] 
于 2012-04-13T23:13:37.207 回答