过滤掉字典列表是否包含集合中的键

Filtering out if the dictionary list has the key in a set

events=[
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'},  'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}]},
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T08:00:00-0700', 'timeZone': 'America/Vancouver'}, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}]},
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-17T00:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-17T00:30:00-0700', 'timeZone': 'America/Vancouver'}}
]
   

我想过滤一个字典列表(列表结构略有不同),如果它有一个 ['attendee']['email'] 是有效的(在电子邮件中)不是组织者。所以 ['attendees']['organizer'] 不能是使用的那个。

emails=['b@hotmail.com'] 
for e in events:
    if e['attendees']['email'] in emails:
        print(e)

生产

TypeError at /
list indices must be integers or slices, not str

应该输出:

 {'kind': 'calendar#event', 'etag': '"1"', 'id': '1', 'status': 'confirmed', 'htmlLink': 'https://www.google.com/calendar/event?eid=1', 'created': '2022-03-14T00:08:11.000Z', 'updated': '2022-03-14T00:08:12.162Z', 'summary': 'Appointment', 'description': 'Online appointment', 'location': 'Online', 'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'iCalUID': '1@google.com', 'sequence': 0, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}], 'reminders': {'useDefault': False, 'overrides': [{'method': 'email', 'minutes': 1440}, {'method': 'popup', 'minutes': 10}]}, 'eventType': 'default'},
 {'kind': 'calendar#event', 'etag': '"1"', 'id': '1', 'status': 'confirmed', 'htmlLink': 'https://www.google.com/calendar/event?eid=1', 'created': '2022-03-14T00:08:11.000Z', 'updated': '2022-03-14T00:08:12.162Z', 'summary': 'Appointment', 'description': 'Online appointment', 'location': 'Online', 'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T08:00:00-0700', 'timeZone': 'America/Vancouver'}, 'iCalUID': '1@google.com', 'sequence': 0, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}], 'reminders': {'useDefault': False, 'overrides': [{'method': 'email', 'minutes': 1440}, {'method': 'popup', 'minutes': 10}]}, 'eventType': 'default'}

e['attendees'] 是一个列表所以你需要指定它的索引

for e in events:
    try:
        if e['attendees'][0]['email'] in emails:
            print(e)
    except KeyError:
        pass

您的第三个条目不包含 'attendees' 字段。您的第 3 行代码将因此而失败。

如果我没理解错的话,您只想打印出包含电子邮件地址包含在 'emails' 列表中的与会者的事件。如果这是真的,此代码将适用于您(假设没有与会者是合法的情况):

for e in events:
    isValid = True
    if e.__contains__("attendees"):
        for a in e["attendees"]:
            if a["email"] in emails:
                print(e)