如何使用Python查找所有输入字符串中的单词?
在python中,是否存在比较两行不同文本以查看两个或更多单词是否匹配的方法如何使用Python查找所有输入字符串中的单词?,python,text,keyword,words,Python,Text,Keyword,Words,在python中,是否存在比较两行不同文本以查看两个或更多单词是否匹配的方法 非常感谢您可以使用集合并计算交点: >>> a = "one two three" >>> b = "one three four" >>> set(a.split()) & set(b.split()) set(['three', 'one']) >>> 您可以拆分每一行以获得出现的单词列表。然后比较这些列表以检查常见单词 def g
非常感谢您可以使用集合并计算交点:
>>> a = "one two three"
>>> b = "one three four"
>>> set(a.split()) & set(b.split())
set(['three', 'one'])
>>>
您可以拆分每一行以获得出现的单词列表。然后比较这些列表以检查常见单词
def get_common_words_count(str1, str2):
list1 = str1.split()
list2 = str2.split()
c = 0
for word in list1:
try:
list2.index(word)
c += 1
except ValueError:
pass
return c
print get_common_words_count('this is the first', 'and this is the second')