I have problem with compressed counting values in one dictionary based of value of another one.
I made up such a code below which represent idea of:
1) Extracting the data to list
2) Taking uniqe values for next proccesing
3) Loop for counting the number of males and females only for "accident"
Problem:
What is the effective solution to counting the values for each category in uniqe set. I mean what if I had 1000 uniqe categories, I do not want to write 1000 "if's"
It's my first question in stackoverflow, that's why i'm sorry for any mistake I've done :)
Original data (first 5 rows):
[
['', 'year', 'month', 'intent', 'police', 'sex', 'age', 'race', 'hispanic', 'place', 'education'],
['1', '2012', '01', 'Suicide', '0', 'M', '34', 'Asian/Pacific Islander', '100', 'Home', '4'],
['2', '2012', '01', 'Suicide', '0', 'F', '21', 'White', '100', 'Street', '3'],
['3', '2012', '01', 'Suicide', '0', 'M', '60', 'White', '100', 'Other specified', '4'],
['4', '2012', '02', 'Suicide', '0', 'M', '64', 'White', '100', 'Home', '4']
]
# Accidents list
accidents_list = [row[3] for row in data] # list of all accidents
print(set(accidents_list)) # unique set
{'Homicide', 'NA', 'Undetermined', 'Accidental', 'Suicide'}
gender_list = [row[5] for row in data]
print(gender_list)
['M', 'F', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'F', 'F', 'M', 'M' ....]
# Accidents dict and loop over it
accidents_gender = {}
for i, v in enumerate(gender_list):
if v not in accidents_gender:
accidents_gender[v] = 0
if accidents_list[i] == 'Accidental':
accidents_gender[v] += 1
print(accidents_gender) # printing only values for accidental
{'M': 1421, 'F': 218}