更新时间:2023-11-28 18:43:04
您可以将句子拆分为单词列表,并使用list
的index
方法:
You can split your sentence to list of words and use index
method of list
:
sentence = "the foo and the bar is foo bar"
words = sentence.split()
def get_distance(w1, w2):
if w1 in words and w2 in words:
return abs(words.index(w2) - words.index(w1))
更新以统计所有单词出现的次数:
Update to count all word occurrences:
import itertools
def get_distance(w1, w2):
if w1 in words and w2 in words:
w1_indexes = [index for index, value in enumerate(words) if value == w1]
w2_indexes = [index for index, value in enumerate(words) if value == w2]
distances = [abs(item[0] - item[1]) for item in itertools.product(w1_indexes, w2_indexes)]
return {'min': min(distances), 'avg': sum(distances)/float(len(distances))}