Python中基于累加模式从JSON生成三类犯罪统计字典的技术问询
Hey there! Let's work through this problem of aggregating your crime data into the three target dictionaries you need. I'll break down each function with clear logic and code that fits your requirements.
First, just to confirm the setup you already have:
import json # Load the crime data with open('/content/data_crime.json', 'r') as f: data_list = json.load(f)
1. Function to Generate murder_by_region (or state)
This function will tally up murder counts grouped by either region or state, using the three parameters you specified: grouping key, crime type, and the data list.
def get_murder_stats(group_key, crime_type, data_list): murder_totals = {} for city_entry in data_list: # Get the group value (e.g., "Northeast" or "California") group_value = city_entry[group_key] # Get the murder count for this city (default to 0 if missing) murder_count = city_entry.get(crime_type, 0) # Accumulate the count for the group if group_value in murder_totals: murder_totals[group_value] += murder_count else: murder_totals[group_value] = murder_count return murder_totals
Usage example:
# Get murder counts grouped by region murder_by_region = get_murder_stats('region', 'Murder', data_list) # Or group by state instead murder_by_state = get_murder_stats('state', 'Murder', data_list)
2. Function to Generate violent_by_region (or state)
Violent crime includes both Murder and Assault, so this function will sum those two types for each group. We'll still use the three parameters, with the crime parameter being a list of the violent crime types.
def get_violent_crime_stats(group_key, crime_types, data_list): violent_totals = {} for city_entry in data_list: group_value = city_entry[group_key] # Calculate total violent crime for this city total_violent = sum(city_entry.get(crime, 0) for crime in crime_types) # Accumulate the total for the group if group_value in violent_totals: violent_totals[group_value] += total_violent else: violent_totals[group_value] = total_violent return violent_totals
Usage example:
# Get violent crime counts grouped by region violent_by_region = get_violent_crime_stats('region', ['Murder', 'Assault'], data_list)
3. Function to Generate nonviolent_by_region (or state)
Non-violent crime covers Theft and Vehicle_Theft. This function follows the same pattern as the violent crime one, just targeting those two types.
def get_nonviolent_crime_stats(group_key, crime_types, data_list): nonviolent_totals = {} for city_entry in data_list: group_value = city_entry[group_key] # Calculate total non-violent crime for this city total_nonviolent = sum(city_entry.get(crime, 0) for crime in crime_types) # Accumulate the total for the group if group_value in nonviolent_totals: nonviolent_totals[group_value] += total_nonviolent else: nonviolent_totals[group_value] = total_nonviolent return nonviolent_totals
Usage example:
# Get non-violent crime counts grouped by region nonviolent_by_region = get_nonviolent_crime_stats('region', ['Theft', 'Vehicle_Theft'], data_list)
Quick Test with Sample Data
If your data_list looks something like this:
data_list = [ {"region": "Northeast", "state": "NY", "Murder": 120, "Assault": 850, "Theft": 2100, "Vehicle_Theft": 300}, {"region": "Northeast", "state": "MA", "Murder": 45, "Assault": 320, "Theft": 1500, "Vehicle_Theft": 180}, {"region": "West", "state": "CA", "Murder": 200, "Assault": 1200, "Theft": 3500, "Vehicle_Theft": 600} ]
Running murder_by_region would return:
{"Northeast": 165, "West": 200}
And violent_by_region would give:
{"Northeast": 1335, "West": 1400}
All functions are flexible enough to switch between grouping by region or state just by changing the first parameter, which fits your original requirement perfectly.
内容的提问来源于stack exchange,提问作者TheDeshBhakt

