检查字符串列表是否包含来自另一个列表的字母

时间:2018-11-13 13:24:12

标签: python string python-3.x dictionary counter

所以我有两个列表:

vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']

我想计算词汇中的字母出现在列表句子的字符串中的次数。

所以我希望输出为:

a = 4
b = 5
c = 6

我的程序:

counter = Counter()
for word in sentences:
    if word in vocabulary:
        counter.update(word)
print(counter)

但是我一直得到输出:

Counter()

3 个答案:

答案 0 :(得分:2)

Counterdict的子类。 dict.update接受其他字典或成对的迭代。但是您只提供了一个字符。

在这种情况下,您可以链接字符串列表并传递到Counter,然后通过字典理解过滤结果:

from collections import Counter
from itertools import chain

vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']

vocab_set = set(vocabulary)
c = Counter(chain.from_iterable(sentences))
res = {k: v for k, v in c.items() if k in vocab_set}

{'a': 4, 'b': 5, 'c': 6}

答案 1 :(得分:1)

这可以做到,不需要import

vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']

data = ''.join(sentences)

for v in vocabulary:
    print('{}: {}'.format(v, data.count(v)))

a: 4
b: 5
c: 6

答案 2 :(得分:1)

没有<import O(n)解决方案:

vocabulary = ['a', 'b', 'c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']

counts = {}
vocab_set = set(vocabulary)
for sentence in sentences:
    for ch in sentence:
        if ch in vocab_set:
            counts[ch] = counts.get(ch, 0) + 1

print(counts)

输出

{'c': 6, 'a': 4, 'b': 5}