簡體   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