我是一个新手,我想编写一个函数来输出包含特定元素的子列表的数量。但我的函数只是输出所有子列表中该特定术语的总数。
我的功能:
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
关于python - 计算在python中具有特定术语的子列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28533505/