Currently have a Dictionary that is structured as such
data =
{
"Invitees": [
{
"firstName": "Will",
"lastName": "Klein",
"email": "WKlein@***il.com",
"highSchool": "NorthwestHS",
"availableDates": [
"2018-02-01",
"2018-02-02"
]
},
{
"firstName": "Jack",
"lastName": "Smith",
"email": "JSmith@***il.com",
"highSchool": "SouthwestHS",
"availableDates": [
"2018-02-04",
"2018-01-01"
]
},
{
"firstName": "Jamie",
"lastName": "Farough",
"email": "JFarough@***il.com",
"highSchool": "NorthwestHS",
"availableDates": [
"2018-02-01",
"2018-02-02"
]
},
{
"firstName": "Bob",
"lastName": "Saggot",
"email": "BSaggot@***il.com",
"highSchool": "NorthwestHS",
"availableDates": [
"2018-02-01",
"2018-02-02"
]
}
]
}
I want to invite people to my 2-day Birthday Party, but I have to pick a date that best works for the individuals (indexed by their HS). The goal is create a program that can index the Dictionary and return which people can attend based on the dates their availability (2 consecutive days). Right now I created a list detailing the information of each person, but am not sure how to combine them by their available dates. I have:
for index in range(lengthofDic):
highSchool.append(data["Attendee"][index]["highSchool"])
email.append(data["Attendee"][index]["email"])
availDate.append(data["Attendee"][index]["availableDates"])
Which gives the different list containing the information for each person, but I dont know how to combine and filter the list in order to find the best 2 dates that work for people at the same school.
Also tried a function below to filter out the same dates between different values in the list, but dont know how to do it for more than 2 list:
for index in range(lengthofDic):
print (lamda x,y: x in (availableDate[1]), (availableDate[1])) #availableDate is list containing dates
An example of the output that I would want is below:
{
"Attendee": [
{
"aCount": 3,
"attendees": [
"BSaggot@***il.com"
"JFarough@***il.com"
"WKlein@***il.com
],
"highSchool": "NorthwestHS",
"startDate": "2018-02-01"
},
{
"aCount": 0,
"attendees": [],
"highSchool": "SouthWestHS,
"startDate": null
}
Southwest High School could not attend because the attendee(s) from that school did not have any consecutive 2 day availability so it returned NULL. Any advice on how to structure the program or what methods to use would be greatly appreciated!!
You can use itertools.groupby
:
import itertools
data = {'Invitees': [{'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Klein', 'highSchool': 'NorthwestHS', 'email': 'WKlein@***il.com', 'firstName': 'Will'}, {'availableDates': ['2018-02-04', '2018-01-01'], 'lastName': 'Smith', 'highSchool': 'SouthwestHS', 'email': 'JSmith@***il.com', 'firstName': 'Jack'}, {'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Farough', 'highSchool': 'NorthwestHS', 'email': 'JFarough@***il.com', 'firstName': 'Jamie'}, {'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Saggot', 'highSchool': 'NorthwestHS', 'email': 'BSaggot@***il.com', 'firstName': 'Bob'}]}
new_data = [(a, list(b)) for a, b in itertools.groupby(sorted(data['Invitees'], key=lambda x:int(x['availableDates'][-1].split('-')[-1])-int(x['availableDates'][0].split('-')[-1]), reverse=True), key=lambda x:int(x['availableDates'][-1].split('-')[-1])-int(x['availableDates'][0].split('-')[-1]))]
final_students = {'Attendee':[{"aCount":len(b), "attendees":[c['email'] for c in b], "highschool":[c['highSchool'] for c in b][0], "startDate":[c['availableDates'][0] for c in b][0]} if a == 1 else {'aCount':0, 'attendees':[], 'highschool':[i['highSchool'] for i in b][0], 'startdate':None} for a, b in new_data]}
Output:
{'Attendee': [{'aCount': 3, 'startDate': '2018-02-01', 'highschool': 'NorthwestHS', 'attendees': ['WKlein@***il.com', 'JFarough@***il.com', 'BSaggot@***il.com']}, {'aCount': 0, 'startdate': None, 'highschool': 'SouthwestHS', 'attendees': []}]}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments