简体   繁体   English

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

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

I am trying to return a list of dictionaries exept for the dictionaries with the same id's as in game_list_id我正在尝试返回与 game_list_id 具有相同 ID 的字典的字典列表

This is what i got so far:这是我到目前为止得到的:

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

You can do that in a single line without a function:您可以在没有 function 的情况下在一行中执行此操作:

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

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

相关问题 如果项目中的值重复,如何从字典列表中删除字典 - How to remove dictionary from list of dictionary if value repeats in items Python:列表由字典项组成,如果列表中的值也从列表中删除键 - Python: list consist of dictionary items, remove key from list if in list is also it's value 如何将列表的每个项目转换为字典? - How to convert each items of list into a dictionary? 如何从列表的项目中创建嵌套字典? - How to make a nested dictionary from a list's items? 如何从字典中的列表中删除最后一项 - How to delete last items from a list that's in a dictionary 如何将存储的字典值列表项匹配到单独列表中的项? - How to match dictionary value list items stored to items in a separate list? 如何从列表中删除项目? - How to remove items from a list? 如何使用python从列表中获取字典,将列表条目作为键,并将列表中的项数作为值? - How do I obtain a dictionary from a list, with list entrys as key and the amount of the items in the list as value, using python? 如何将字典列表转换为列表值中每个字典的列表? - How to convert a list of dictionaries into a list of each dictionary in the list's values? 解析字典中的字典列表以从每个字典中检索特定键的值 - Parsing list of dictionaries in a dictionary to retrieve a specific key's value from each dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM