I’m struggling to figure out how to find the most visited person
each month. The code below is taken initially from here with some tweaks:
# Function to extract maximum visits of an individual
def max_person(data):
person = {}
for row in data:
name = row[0]
person[name] = person.get(name, 0) + 1
return [max(person, key = person.get), person[max(person, key = person.get)]]
# Store months from "potus" data set
months = []
for row in potus:
month_name = row[2].strftime('%b')
if month_name not in months:
months.append(month_name)
# Finding the most visited person in each month
for month in months:
visitors_list = []
for row in potus:
month_name = row[2].strftime('%b')
if month_name == month:
visitors_list.append(row)
print([month] + max_person(visitors_list))
Output:
['Jan', 'Jesus MurilloKaram', 3]
['Feb', 'Sheila JacksonLee', 5]
['Mar', 'Anna M. Yeo', 3]
['Apr', 'Alan C. Prather', 7]
['May', 'Chris Coons', 6]
['Jun', 'Marilda W. Averbug', 6]
['Jul', 'Russell A. Wilson', 4]
['Aug', 'ELIZABETH C. NUNEZ', 3]
['Sep', 'Shaojun ■■■■', 6]
['Oct', 'AnnaMaria R. Mottola', 8]
['Nov', 'MICHAEL LISZEWSKI', 3]
['Dec', 'Brian E. Fallon', 4]
I’m not satisfied with this approach. I believe this can be done with nested dictionaries but I have been struggling to build the logic since yesterday. I am looking for something like:
{'Jan': {'Jesus MurilloKaram': 3},
'Feb': {'Sheila JacksonLee': 5},
'Mar': {'Anna M. Yeo': 3},
......
'Dec': {'Brian E. Fallon': 4}}
I am open to any idea to improve my solution. Thank you all for your consideration.