计算在 python 中具有特定术语的子列表

count sublists that has a specific term in python

我是新手,我想编写一个函数来输出包含特定元素的子列表的计数。但是我的函数只输出所有子列表中该特定术语的总数。

我的函数:

def count(myList):
    tmp = []
    d = {}
    for item in myList: tmp += item
    for key in tmp: d[key] = d.get(key, 0) + 1
    return d

我的输出:

>>res = count_doc_frequencies([['a', 'b', 'a'], ['a', 'b', 'c'], ['a']])
>>res['a']
4
>>res['b']
2

期望的输出:

>>res = count_doc_frequencies([['a', 'b', 'a'], ['a', 'b', 'c'], ['a']])
>>res['a']
3

因为 'a' 出现在 3 个子列表中..

任何人都可以帮我修改我的函数以获得所需的输出吗??

lst = [['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]

def count(lst):
    # declare dictionary that we are going to return
    foo = {}
    # iterate sublist
    for sublist in lst:
        # make sublist into unique element list
        sublist = list(set(sublist))
        for element in sublist:
            # if element found in foo dic, increment
            if element in foo:
                foo[element] += 1
            # else, init with 1
            else:
                foo[element] = 1
    return foo

res = count(lst)
print res

您应该更改此声明

tmp += item

tmp += set(item)

这将消除子列表中元素的重复计数。

另一种写法是

def count(myList,ele):
tmp = []
key = 0
for item in myList:
    if ele in item:
        key += 1
return key