Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python查找所有输入字符串中的单词?_Python_Text_Keyword_Words - Fatal编程技术网

如何使用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

在python中,是否存在比较两行不同文本以查看两个或更多单词是否匹配的方法


非常感谢

您可以使用集合并计算交点:

>>> 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')