如果字典列表中包含某个集合中的关键字,则进行筛选


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'}}
]

我想过滤一个字典列表(列表结构略有不同(,如果它有一个有效的[‘attende’][‘email’](在电子邮件中(,而不是组织者。因此,不能使用["与会者"]["组织者"]。

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

您的第三个条目不包含"与会者"字段。您的第三行代码将因此而失败。

如果我理解正确,您只想打印出包含"电子邮件"列表中包含其电子邮件的与会者的活动。如果这是真的,这个代码将为你工作(假设没有与会者是合法的情况(:

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

最新更新