如果变量匹配list元素的百分比,则执行某些操

时间:2018-04-10 16:35:02

标签: python python-3.x

很抱歉,如果我事先解释得不好。如果变量匹配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%的单词,那么它会做一些事情。如果您无法理解我的问题,请再次进行。自己无法理解它。

1 个答案:

答案 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