繁体   English   中英

对于列表中的每个值,如何从字典列表中删除项目

[英]How to remove items from dictionary list, for each value that's in a list

我正在尝试返回与 game_list_id 具有相同 ID 的字典的字典列表

这是我到目前为止得到的:

list_games = [
    {'id': 10, 'name_game': 'x1'},
    {'id': 20, 'name_game': 'x2'},
    {'id': 30, 'name_game': 'x3'},
    {'id': 40, 'name_game': 'x4'},
    {'id': 50, 'name_game': 'x5'},
    {'id': 60, 'name_game': 'x6'},
    {'id': 70, 'name_game': 'x7'},
    {'id': 80, 'name_game': 'x8'},
    {'id': 90, 'name_game': 'x9'}
]

game_id_list = [10, 30, 40]


def remove_game_id_list(list_games, game_id_list):
    results = []
    for b in game_id_list:
        for i in range(len(list_games)):
            if list_games[i]['id'] != b:
                results.append(list_games[i])

    return results

您可以在没有 function 的情况下在一行中执行此操作:

result = [dct for dct in list_games if dct['id'] not in game_id_list]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM