很抱歉,如果我事先解释得不好。如果变量匹配list元素的某个百分比,我想输出一些东西。例如:
sentencelst = ['It is a very nice day', 'How are you', 'How is the weather', 'Have a great day']
matchedsentence = 'It is a nice day'
if matchedsentence is 75% similar in sentencelst:
print('75% match')
else:
print('less than 75% match')
continue
最终目标是,如果matchedsentence
包含sentencelst
中任何元素的75%的单词,那么它会做一些事情。如果您无法理解我的问题,请再次进行。自己无法理解它。
答案 0 :(得分:2)
您可以使用fuzzywuzzy
包:
from fuzzywuzzy import fuzz
sentencelst = ['It is a very nice day', 'How are you', 'How is the weather', 'Have a great day']
matchedsentence = 'It is a nice day'
for sentence in sentencelst:
if fuzz.ratio(sentence, matchedsentence) > 75:
print('75% match')
else:
print('less than 75% match')
continue