2011-11-05 26 views
2

我的單,多詞短語的列表字符串:的Python:檢測共享的話

terms = ['Electronic rock', 'Alternative rock', 'Indie pop']

我想檢測terms[0]terms[1]份額字rock。有沒有Pythonic的方式來做到這一點,而不是使用大量的for-loops,臨時列表和split(' ')

基本上,我試圖檢測短語的半平等。

回答

6

可以使用dictonary記住哪些詞出現在哪些條款:

from collections import defaultdict 

terms = ['Electronic rock', 'Alternative rock', 'Indie pop'] 
d = defaultdict(list) 
for term in terms: 
    for word in term.split(): 
     d[word].append(term) 

for k,v in d.iteritems(): 
    if len(v) > 1: 
     print k,v 

輸出:

 
rock ['Electronic rock', 'Alternative rock'] 

看到它聯機工作:ideone

+0

哈哈我是一半打字幾乎完全一樣的東西...也許一個2.7 +/3 +傢伙會告訴我們一個更簡潔計數器例? – Triptych

+0

那該死的美麗 –

+0

不錯的作品表現出對字典的有效使用。 –

1

這是一個非常低效的解決方案對於這些簡單的列表元素,但對於較長的字符串,您可以使用itertools' combinations生成一組2列表列表,然後使用difflib比較字符串。如果你只是處理兩三個單詞,這個解決方案不適合你。

1

訪問How to find list intersection? 我認爲答案可以從這個角度思考。在你的問題中,我們不知道你想表達什麼結果。我想你最好列出你想得到的結果。

這裏我列出可以給你一些提示的結果。 (好吧,沒有分裂,我不認爲這是明確的理解)。

a=terms[0].split() 
b=terms[1].split() 
list(set(a) & set(b)) 
1

上@MarkByers的答案的一些變化:

>>> from collections import defaultdict 
>>> 
>>> terms = [ 
...  'Electronic rock', 'Alternative rock', 'Indie pop', 
...  'baa baa black sheep', 
...  'Blackpool rock', # definition of "equality"? 
...  'Rock of ages', 
...  ] 
>>> 
>>> def process1(): 
...  d = defaultdict(list) 
...  for term in terms: 
...   for word in term.split(): 
...    d[word].append(term) 
...  for k,v in d.iteritems(): 
...   if len(v) > 1: 
...    print k,v 
... 
>>> def process2(): 
...  d = defaultdict(set) 
...  for term in terms: 
...   for word in term.split(): 
...    d[word.lower()].add(term) 
...  for k,v in d.iteritems(): 
...   if len(v) > 1: 
...    print k, sorted(list(v)) 
... 
>>> process1() 
rock ['Electronic rock', 'Alternative rock', 'Blackpool rock'] 
baa ['baa baa black sheep', 'baa baa black sheep'] 
>>> process2() 
rock ['Alternative rock', 'Blackpool rock', 'Electronic rock', 'Rock of ages'] 
>>>