listin中list1的Python count元素出现

时间:2017-02-15 16:50:22

标签: python list counting

在下面的代码中,我想计算word_listtest中每个单词的出现次数,下面的代码可以完成这项工作,但可能效率不高,有没有更好的方法做到了吗?

word_list = ["hello", "wonderful", "good", "flawless", "perfect"]
test = ["abc", "hello", "vbf", "good", "dfdfdf", "good", "good"]

result = [0] * len(word_list)
for i in range(len(word_list)):
    for w in test:
        if w == word_list[i]:
            result[i] += 1

print(result)

4 个答案:

答案 0 :(得分:6)

使用collections.Counter一次性计算test中的所有字词,然后只需Counterword_list中每个字的计数。

>>> word_list = ["hello", "wonderful", "good", "flawless", "perfect"]
>>> test = ["abc", "hello", "vbf", "good", "dfdfdf", "good", "good"]
>>> counts = collections.Counter(test)
>>> [counts[w] for w in word_list]
[1, 0, 3, 0, 0]

或使用字典综合:

>>> {w: counts[w] for w in word_list}
{'perfect': 0, 'flawless': 0, 'good': 3, 'wonderful': 0, 'hello': 1}

创建计数器应为O(n),并且每个查找O(1),为test中的n个单词和word_list中的m个单词提供O(n + m)。

答案 1 :(得分:3)

你可以使用字典在线性时间内完成。

TASK [Debug] *******************************************************************
ok: [127.0.0.1] => {
    "msg": "NAME: abcd PASS: defg USER: fghi"
}

答案 2 :(得分:1)

您可以合并collections.Counteroperator.itemgetter

from collections import Counter
from operator import itemgetter

cnts = Counter(test)
word_cnts = dict(zip(word_list, itemgetter(*word_list)(cnts)))

给出了:

>>> word_cnts
{'flawless': 0, 'good': 3, 'hello': 1, 'perfect': 0, 'wonderful': 0}

或者您希望将其作为list

>>> list(zip(word_list, itemgetter(*word_list)(cnts)))
[('hello', 1), ('wonderful', 0), ('good', 3), ('flawless', 0), ('perfect', 0)]

答案 3 :(得分:-1)

您可以尝试使用词典:

word_list = ["hello", "wonderful", "good", "flawless", "perfect"]
test = ["abc", "hello", "vbf", "good", "dfdfdf", "good", "good"]

result = {}
for word in word_list:
    result[word]=0
for w in test:
    if result.has_key(w):
        result[w] += 1
print(result)

但你会以不同的结构结束。 如果你不想那样,你可以试试这个

word_list = ["hello", "wonderful", "good", "flawless", "perfect"]
test = ["abc", "hello", "vbf", "good", "dfdfdf", "good", "good"]

result = {}
for w in test:
    if(result.has_key(w)):
        result[w] += 1
    else:
        result[w] = 1
count = [0] * len(word_list)
for i in range(len(word_list)):
    if (result.has_key(word_list[i])):
        count[i]=result[word_list[i]]
print(count)