2014-03-27 59 views

回答

2

你可以只喂Counter生成器表達式:

cnt = Counter(word for sublist in multiple for word in sublist) 

cnt 
Out[40]: Counter({'apple': 3, 'ball': 2, 'cat': 2}) 

sum(cnt.values()) 
Out[41]: 7 
名單是巨大的,所以我使用的只是一個虛擬實例

multiple=[['apple','ball','cat']['apple','ball']['apple','cat'].......] 
words=['apple','ball','cat','duck'......] 
word = 'apple' 
cnt = Counter() 
total = 0 
for i in multiple: 
     for j in i: 
      if word in j: 
       cnt[word] +=1 
       total += cnt[word] 

我想這樣的輸出中的對象列表

我並沒有真正看到您的words列表。你沒有使用它。

如果您需要過濾掉不在words的話,讓words一個set一個list

words = {'apple','ball','cat','duck'} 

cnt = Counter(word for sublist in multiple for word in sublist if word in words) 

否則你會得到O(n ** 2)的行爲應該是O(n)操作。

0

這工作在Python 2.7和Python 3.x的:

from collections import Counter 

multiple=[['apple','ball','cat'],['apple','ball'],['apple','cat']] 
words=['apple','ball','cat','duck'] 
cnt = Counter() 
total = 0 
for i in multiple: 
     for word in i: 
      if word in words: 
       cnt[word] +=1 
       total += 1 
print cnt #: Counter({'apple': 3, 'ball': 2, 'cat': 2}) 
print dict(cnt) #: {'apple': 3, 'ball': 2, 'cat': 2} 
print total #: 7 
print sum(cnt.values()) #: 7 

在Python 2.x中,你應該使用的.itervalues()代替.values()即使兩者的工作。

短一點的解決方案,基於roippi的回答是:

from collections import Counter 
multiple=[['apple','ball','cat'],['apple','ball'],['apple','cat']] 
cnt = Counter(word for sublist in multiple for word in sublist) 
print cnt #: Counter({'apple': 3, 'ball': 2, 'cat': 2}) 
相關問題