Count the number of elements in a JSON element

I use Python to parse the English police API. I want to analyze the JSON response I get in order to calculate how many times a certain violation occurs. This is an example API response.

{ category: "anti-social-behaviour", location_type: "Force", location: { latitude: "53.349920", street: { id: 583315, name: "On or near Evenwood Close" }, longitude: "-2.657889" }, context: "", outcome_status: null, persistent_id: "", id: 22687179, location_subtype: "", month: "2013-03" }, 

Using this code

 from json import load from urllib2 import urlopen import json url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03" json_obj = urlopen(url) player_json_list = load(json_obj) for player in player_json_list: crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': ')) print crimeCategories 

I get an answer like this

 "anti-social-behaviour" "anti-social-behaviour" "anti-social-behaviour" "anti-social-behaviour" "drugs" "drugs" "burglary" 

If I changed the for loop to

 for player in player_json_list: crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': ')) print crimeCategories.count("drugs") 

Then I get a response like

 0 0 0 0 1 1 0 

Searching the forum for hours didn't help me! Any ideas?

+6
source share
4 answers

You can use collection.Counter dict with queries that become a few short lines of code:

 import requests from collections import Counter url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03" json_obj = requests.get(url).json() c = Counter(player['category'] for player in json_obj) print(c) 

Output:

 Counter({'anti-social-behaviour': 79, 'criminal-damage-arson': 12, 'other-crime': 11, 'violent-crime': 9, 'vehicle-crime': 7, 'other-theft': 6, 'burglary': 4, 'public-disorder-weapons': 3, 'shoplifting': 2, 'drugs': 2}) 

If you prefer to have a normal voice recorder, then just call dict on the dict counter:

 from pprint import pprint as pp c = dict(c) pp(c) 
 {'anti-social-behaviour': 79, 'burglary': 4, 'criminal-damage-arson': 12, 'drugs': 2, 'other-crime': 11, 'other-theft': 6, 'public-disorder-weapons': 3, 'shoplifting': 2, 'vehicle-crime': 7, 'violent-crime': 9} 

Then you just access the c['drugs'] key, etc.

Or iterate over the elements for printing crime and counting in the desired format:

 for k, v in c.items(): print("{} count: {}".format(k, v) 

Output:

 drugs count: 2 shoplifting count: 2 other-theft count: 6 anti-social-behaviour count: 79 violent-crime count: 9 criminal-damage-arson count: 12 vehicle-crime count: 7 public-disorder-weapons count: 3 other-crime count: 11 burglary count: 4 
+7
source

Create a dictionary and use the crimeCategories keyword. Use an integer for the value. Try putting something like this in your loop.

 >>> count['testing'] = count.get('testing',0) + 1 >>> count['testing'] 1 
0
source

You do not store your account anywhere. Basically you just call the count of an element in your loop.

You need to add each element as a key in the dictionary, and then increment the value each time you press find in your for loop

 adictionary = {"drugs":0} for player in player_list: if adictionary.category adictionary.category += 1 print adictionary.category 
0
source

You can combine the data with a category-> json mapping as follows:

 from collections import defaultdict players_by_category = defaultdict(list) for player in players_json_list: players_by_category[player['category'].append(player) 

And now you have a dictionary with a list of crimes for each category.

So, to find out how many crimes in a certain category have occurred, you need:

 for k, v in players_by_category.iteritems(): print "%s: %s" (k, len(v)) 

It is very difficult to use player instead of crime , but everything that you consider necessary :)

0
source

Source: https://habr.com/ru/post/983239/


All Articles