[英]Finding nested list with identical values in Dictionaries [Python]
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). 我想邀请人们参加我的2天生日派对,但是我必须选择一个最适合个人的日期(以他们的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). 目标是创建一个程序,该程序可以对字典进行索引并根据其可用日期(连续2天)返回人们可以参加的字典。 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. 这给出了包含每个人的信息的不同列表,但是我不知道如何组合和过滤该列表以找到适合同一所学校的人的最佳2个日期。
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. 西南高中无法参加,因为该学校的参加者没有连续2天的可用时间,因此返回NULL。 Any advice on how to structure the program or what methods to use would be greatly appreciated!! 任何有关如何构造程序或使用哪种方法的建议,将不胜感激!!
You can use itertools.groupby
: 您可以使用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': []}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.