简体   繁体   English

计算列表列表中的出现次数

[英]counting occurrences in list of list

Why this does not work?为什么这不起作用? I found the functions written under on Stack Overflow, but it still doesn't work.我在 Stack Overflow 上找到了写在下面的函数,但它仍然不起作用。

I ideally want to find how many times the word occurs in the inner list and how many times all the words occurs in all the lists combined.理想情况下,我想找出单词在内部列表中出现的次数以及所有单词在所有列表中出现的次数。 So for 'das' - it occurs two times in the first list of the list and three times total:因此,对于 'das' - 它在列表的第一个列表中出现两次,总共出现 3 次:

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)

The error I get is:我得到的错误是:

Traceback (most recent call last):
  File "it.py", line 45, in <module>
    occur_dict(a)
  File "it.py", line 28, in occur_dict
    if i in d:
TypeError: unhashable type: 'list'

We have some list of lists a .我们有一些列表a We want to get counts for each of the sublists, and the total of those counts.我们想要获得每个子列表的计数,以及这些计数的总数。 Note that the total counts will just be the sum of the counts of the sublists.请注意,总计数将只是子列表计数的总和。

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

Now if we want the counts of das in the first sublist and the number of das across all sublists we can do现在,如果我们想要第一个子列表中的das计数以及所有子列表中的das数量,我们可以这样做

counts, total = count(a)
print(counts[0]['das'])
# 2
print(total['das'])
# 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM