2014-03-27 33 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}) 
相关问题