2014-01-21 33 views

回答

4

您可以使用集和計算交會:

>>> a = "one two three" 
>>> b = "one three four" 
>>> set(a.split()) & set(b.split()) 
set(['three', 'one']) 
>>> 
+0

做得很好。 +1 –

0

您可以分割每條線獲得所存在的單詞列表。然後比較列表以檢查常用單詞。

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') 
相關問題