简体   繁体   English

如何计算dicts列表中值的出现次数?

[英]How can I count occurrences of values in a list of dicts?

I have this list of dicts: 我有这个dicts列表:

[{'Description': 'LARCENY'}, {'Description': 'LARCENY'}, {'Description': 'BURGLARY'}, {'Description': 'ROBBERY - STREET'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'AUTO THEFT'}, {'Description': 'AUTO THEFT'}, {'Description': 'ROBBERY - STREET'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'BURGLARY'}, {'Description': 'BURGLARY'}, {'Description': 'LARCENY'}, {'Description': 'ROBBERY - COMMERCIAL'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}]

I need to make a function that iterates over those dictionaries and counts how many times each type of crime has occurred. 我需要创建一个迭代这些词典的函数,并计算每种类型的犯罪发生的次数。 The output should be a list of dicts like this: 输出应该是这样的dicts列表:

[{'LARCENY' : 3}, {'BURGLARY' : 2}, {'ROBBERY - STREET' : 3}...

For each type of crime, I need to know how many times that crime as occurred. 对于每种类型的犯罪,我都需要知道犯罪发生的次数。 This is what I have so far: 这是我到目前为止:

result = {}
for k in data:
   if 'Description' in k:
    result[k['Description']] = result.get(k['Description'], 0) + 1 

But the output given gives me everything in the same dict, but I want them to be in a list of dicts, each crime with each dict. 但是给出的输出给了我同一个词中的所有内容,但我希望它们在一个词典列表中,每个词都与每个词典有关。

Feel free to ask any question if you don't understand mine. 如果你不理解我的话,请随意提出任何问题。

Looks like you want to maintain the order of occurrence of the crimes as well. 看起来你想要维持罪行的发生顺序。

So, use collections.OrderedDict instead of normal dict. 因此,使用collections.OrderedDict而不是普通的dict。

>>> from collections import OrderedDict
>>> counter = OrderedDict()
>>> for item in data:
...     if 'Description' in item:
...         counter[item['Description']] = counter.get(item['Description'], 0) + 1

Now, just construct a new dictionary with each and every key, value pair out of the counter , like this 现在,只需构建一个新的字典,其中包含counter外的每个键值对,就像这样

>>> from pprint import pprint
>>> pprint([{k: v} for k, v in counter.items()])
[{'LARCENY': 3},
 {'BURGLARY': 3},
 {'ROBBERY - STREET': 2},
 {'COMMON ASSAULT': 7},
 {'AUTO THEFT': 2},
 {'ROBBERY - COMMERCIAL': 1}]

From the comments, 从评论中,

After I get the desired output I will need to separate the keys and the values into diferent lists, 获得所需的输出后,我需要将键和值分成不同的列表,

Then, don't create list of dictionaries. 然后,不要创建字典列表。 Directly create two lists, with keys and values , from the counter itself, like this 直接从counter本身创建两个带有keysvalues列表,就像这样

>>> list(counter.keys())
['LARCENY', 'BURGLARY', 'ROBBERY - STREET', 'COMMON ASSAULT', 'AUTO THEFT', 'ROBBERY - COMMERCIAL']
>>> list(counter.values())
[3, 3, 2, 7, 2, 1]

Given the result you've got so far... 鉴于你到目前为止的结果......

>>> result = {'LARCENY' : 3, 'BURGLARY' : 2, 'ROBBERY - STREET' : 3}
>>> result = [{k:v} for k,v in result.items()]
>>> result
[{'BURGLARY': 2}, {'LARCENY': 3}, {'ROBBERY - STREET': 3}]

While your data structure doesn't make sense as noted above, you can solve this easily using the Counter class from collections. 如上所述,虽然您的数据结构没有意义,但您可以使用集合中的Counter类轻松解决此问题。

from collections import Counter

crimes = [{'Description': 'LARCENY'}, {'Description': 'LARCENY'}, {'Description': 'BURGLARY'}, {'Description': 'ROBBERY - STREET'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'AUTO THEFT'}, {'Description': 'AUTO THEFT'}, {'Description': 'ROBBERY - STREET'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'BURGLARY'}, {'Description': 'BURGLARY'}, {'Description': 'LARCENY'}, {'Description': 'ROBBERY - COMMERCIAL'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}, {'Description': 'COMMON ASSAULT'}]

c = Counter()
for item in crimes:
    c[item["Description"]] += 1

print(c)

This gives the output of: 这给出了输出:

Counter({'COMMON ASSAULT': 7, 'BURGLARY': 3, 'LARCENY': 3, 'AUTO THEFT': 2, 'ROBBERY - STREET': 2, 'ROBBERY - COMMERCIAL': 1})

I'd recommend looking at the Counter class whenever you want to count things. 我建议你在计算东西的时候查看Counter类。

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

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