过滤掉字典列表是否具有集合中的键

青葱年少 python 264

原文标题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'}

原文链接:https://stackoverflow.com//questions/71462320/filtering-out-if-the-dictionary-list-has-the-key-in-a-set

回复

我来回复
  • Bird_的头像
    Bird_ 评论

    e[‘attendees’] 是一个列表,因此您需要指定它的索引

    for e in events:
        try:
            if e['attendees'][0]['email'] in emails:
                print(e)
        except KeyError:
            pass
    
    2年前 0条评论
  • Ewan Brown的头像
    Ewan Brown 评论

    您的第三个条目不包含“参加者”字段。因此,您的第三行代码将失败。

    如果我对您的理解正确,您只想打印包含参加者的活动,该参加者的电子邮件包含在“电子邮件”列表中。如果这是真的,此代码将适用于您(假设没有与会者是合法的情况):

    for e in events:
        isValid = True
        if e.__contains__("attendees"):
            for a in e["attendees"]:
                if a["email"] in emails:
                    print(e)
    
    2年前 0条评论