Problem
Given a list of feedbacks to questionary, output the statistics report, grouped by ethnic group.
One feedback record contains info like ethnic group, gender, citizenship, etc.
Solution
This is a simple problem. The key here is to choose and clearly define the data structures. Instinctively,
1. Feedback record: One feedback record is naturally a dictionary like {‘ethnic’:'African American’, ‘gender’:'Male’, ‘citizenship’:'US’ }.
2. Statistics: It is a list of statistics record, where each record is identified by ethnic group and contains several statistics fields. Likewise, one statistics record is naturally a dictionary like {‘ethnic’:'African American’, ‘Gender_Male’:3, ‘Gender_Female’:3, ‘citizenship_US’: 4, ‘citizenship_permvisa’: 1, …}
Side note: the statistics table can be generated by SQL-like statement (cause it’s actually aggregate query) like: select ethnic, count(gender == ‘male’) as gender_male, … group by ethnic
Code:
Based on data structure definition, code is straightforward:
def prog_3_A_questionary(l): """ >>> l = [] >>> l.append(dict(ethnic='African American', gender='Male', citizenship='Perm Visa')) >>> l.append(dict(ethnic='African American', gender='Male', citizenship='US Citizen')) >>> l.append(dict(ethnic='African American', gender='Male', citizenship='Perm Visa')) >>> l.append(dict(ethnic='African American', gender=None, citizenship='Perm Visa')) >>> l.append(dict(ethnic='African American', gender='Female', citizenship='Perm Visa')) >>> l.append(dict(ethnic='African American', gender='Female', citizenship='Temp')) >>> l.append(dict(ethnic='African American', gender='Female', citizenship='Perm Visa')) >>> l.append(dict(ethnic='African American', gender='Female', citizenship=None)) >>> l.append(dict(ethnic='Spanish Surname', gender='Female', citizenship='Perm Visa')) >>> l.append(dict(ethnic='Spanish Surname', gender='Female', citizenship='Temp')) >>> l.append(dict(ethnic='Spanish Surname', gender='Female', citizenship='Perm Visa')) >>> l.append(dict(ethnic='Asian American', gender='Male', citizenship='Perm Visa')) >>> from pprint import pprint >>> pprint(prog_3_A_questionary(l)) {'African American': {'Total': 8, 'citizenship_Perm Visa': 5, 'citizenship_Temp': 1, 'citizenship_US Citizen': 1, 'ethnic_African American': 8, 'gender_Female': 4, 'gender_Male': 3}, 'Asian American': {'Total': 1, 'citizenship_Perm Visa': 1, 'ethnic_Asian American': 1, 'gender_Male': 1}, 'Spanish Surname': {'Total': 3, 'citizenship_Perm Visa': 2, 'citizenship_Temp': 1, 'ethnic_Spanish Surname': 3, 'gender_Female': 3}} """ # TODO: actually, we should write code to initialize the stat fields like # 'Gender_Male' to be 0. This will help: # - reduce the runtime check logic to see if the field is already there or not # - initialize all the stat_fields to 0, in order to prevent any missing stat_fields. from collections import defaultdict stat = {} for record in l: # get stat record by 'group key' - ethnic ethnic = record['ethnic'] if not ethnic in stat: stat_record = {} stat[ethnic] = stat_record stat_record = stat[ethnic] # increase 'Total' if not 'Total' in stat_record: stat_record['Total'] = 0 stat_record['Total'] += 1 # increase stat fields like gender_Male for key, val in record.items(): # handle missing answer if val == None: continue stat_field = '%s_%s' % (key, val) if not stat_field in stat_record: stat_record[stat_field] = 0 stat_record[stat_field] += 1 return stat
YOU NEED FAST PROXY SERVERS ?
Check it out this Anonymous and Private Proxy Servers.
HTTP & SOCKS5 Proxy supported.
IP Authentication or Password Authentication available.
HERE: https://bit.ly/3ifZkmL
Hello
YOU NEED QUALITY VISITORS FOR YOUR: pangwa.com
WE PROVIDE ORGANIC VISITORS BY KEYWORD FROM SEARCH ENGINES OR SOCIAL MEDIA
YOU GET HIGH-QUALITY VISITORS
– visitors from search engines
– visitors from social media
– visitors from any country you want
CLAIM YOUR 24 HOURS FREE TEST => https://bit.ly/2HQZggh
Thanks, Claudia Hoad
If you no longer wish to hear from us, please reply this email.
YOU NEED REAL VISITORS FOR: pangwa.com ?
We Provide 100% Real Visitors To Your Website.
With this traffic, you can boost ranking in SERP, SEO, profit from CPM…
CLAIM YOUR 24 HOURS FREE TEST HERE=> https://zeep.ly/Kp4UB