有人可以告诉我为什么这不起作用?
,当我搜索我的问题时,我发现在Stack Overflow上编写了这些函数,但它仍无效。理想情况下,我希望找到内容列表中出现的单词次数以及所有单词组合出现的次数。所以对于' das' - 它在列表的第一个列表中出现两次,总共出现三次:
function updateEventCount() {
$.get("event-count", function(fragment) { // get from controller
$("#eventCount").replaceWith(fragment); // update snippet of page
});
}
我得到的错误是:
from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
a = [['das','sadad','asdas','das'],['das','sadad','da'],['aaa','8.9']]
def counter(a):
return Counter(a)
def count(a):
return dict((i, a.count(i)) for i in set(a))
def bincount(a):
return numpy.bincount(a)
def pandas_value_counts(a):
return pandas.Series(a).value_counts()
def occur_dict(a):
d = {}
for i in a:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d
def count_unsorted_list_items(items):
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)
def operator_countof(a):
return dict((i, operator.countOf(a, i)) for i in set(a))
occur_dict(a)
pandas_value_counts(a)
count_unsorted_list_items(a)
bincount(a)
count(a)
counter(a)
operator_countof(a)
答案 0 :(得分:1)
我们有一些列表a
。我们希望获得每个子列表的计数,以及这些计数的总数。请注意,总计数只是子列表计数的总和。
from collections import Counter
a = [['das','sadad','asdas','das'],['das','sadad','da'],['aaa','8.9']]
def count(list_of_lists):
counts = [Counter(sublist) for sublist in list_of_lists]
total_count = sum(counts, Counter())
return counts, total_count
现在,如果我们希望第一个子列表中的das
计数和所有子列表中das
的数量,我们可以做
counts, total = count(a)
print(counts[0]['das'])
# 2
print(total['das'])
# 3