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

时间:2015-02-16 01:59:18

标签: python list sublist

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

我的功能:

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个子列表中..

任何人都可以帮我修改我的功能以实现所需的输出吗?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您应该更改此声明

tmp += item

tmp += set(item)

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

答案 2 :(得分:0)

另一种写这个的方法是

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