删除嵌套字典中的列表项

时间:2015-03-02 15:10:01

标签: python python-2.7 dictionary

我有一个嵌套字典,如下所示:

{'Div 4 A Herrar': {'0': {'awayScore': '0',
                          'awayTeam': 'Floda BoIF',
                          'events': [],
                          'homeScore': '0',
                          'homeTeam': 'SKIF Semberijaaaah',
                          'tempTime': '10:00',
                          'time': 'FT'},
                    '1': {'awayScore': '0',
                          'awayTeam': 'Kode IF',
                          'events': [{'eventID': '3215',
                                      'eventScore': '1-0',
                                      'eventTime': 'HT',
                                      'eventType': 'Goal-timocin',
                                      'name': 'timocin',
                                      'selectedTeam': 'homeTeam'},
                                     {'eventID': '2312',
                                      'eventScore': '2-0',
                                      'eventTime': 'HT',
                                      'eventType': 'Goal-timocin',
                                      'name': 'timocin',
                                      'selectedTeam': 'homeTeam'}],
                          'homeScore': '2',
                          'homeTeam': 'Partille IF FK',
                          'tempTime': '11:00',
                          'time': 'FT'},
                    '2': {'awayScore': '0',
                          'awayTeam': 'Romelanda UF',
                          'events': [],
                          'homeScore': '0',
                          'homeTeam': 'IK Kongah\\u00e4lla',
                          'tempTime': '12:00',
                          'time': 'FT'}}}

我希望能够删除事件列表中的特定事件。我之前知道的是前两个键的含义:

"Div 4 A Herrar" and the integer number that represents a dictionary item

我还需要删除的事件的 eventID

例如,为了让您更清楚,我们可以说我想删除一个带有eventID 3215的事件。然后我会将此信息删除:

"Div 4 A Herrar", 1, 3215'

如果某些事情不清楚,请询问,我会更新我的问题更具体。

EDIT1

这是我只能想到的事情:

for event in dict["Div 4 A Herrar"][1]["events"]:
    if(event["eventID"] == "3215"):
        # this is where i got stuck since i had no idea how to proceed from here. 

2 个答案:

答案 0 :(得分:1)

def remove_event(dicts, key, id, event_id):
    if key in dicts and id in dicts[key] and "events" in dicts[key][id]:
        dicts[key][id]['events'] = [event for event in dicts[key][id]['events']
                                    if event["eventID"] != event_id]
    else:
        raise ValueError("The actual Events could not be found")

remove_event(data, "Div 4 A Herrar", "1", "3215")

只需使用列表推导过滤events列表中的事件,如上面的函数所示。关键在于,

[event for event in dicts[key][id]["events"] if event["eventID"] != event_id]

它通过迭代dicts[key][id]["events"]创建一个新列表,并且仅当event event不相同时才包含当前eventID字典作为传递给函数的那个​​。

答案 1 :(得分:0)

def remove_event(d,d_id,key,event_id):
    # d = dictionary 
    # d_id = 'Div 4 A Herrar'
    # key = '1'
    # event_id = '3215'

    # create temp list
    lst = []
    # loop the events list
    for event in d[d_id][key]['events']:
        # if event_id does not match
        if event['eventID'] != event_id:
            # append to temp list
            lst.append(event)
    # make that temp list the events list
    d[d_id][key]['events'] = lst