【问题标题】:Given a list of strings, remove an item from list of dicts based on its value给定一个字符串列表,根据它的值从字典列表中删除一个项目
【发布时间】:2022-01-17 22:55:10
【问题描述】:

我有以下字典列表:

money_line = [
            {
              "id": 1,
              "book": "SPORT_888"
            },
            {
              "id": 2,
              "book": "WYNN"
            },
            {
              "id": 3,
              "book": "BET_RIVERS_VA"
            },
            {
              "id": 4,
              "book": "WILLIAM_HILL"
            {
              "id": 5,
              "book": "SUGAR_HOUSE_NJ"
            },
            {
              "id": 6,
              "book": "WYNN_NY"
            }
]

还有以下字符串列表:

list_to_remove = ["SPORT_888", "WYNN", "MGM"]

如您所见,在 dict 值中,我在“WYNN_NY”项目上有一个后缀。我需要从 money_line 中删除 list_to_remove 中的所有项目,忽略后缀。

已经试过了:

live_money_line = [i for i in money_line if i['book'].rsplit('_', 1)[0] not in list_to_remove]

但这会删除“888”“SPORT_888”,这不是我需要的结果。

也试过了:

for code in list_to_remove:
  for item in money_line:
    if code in item['book']:
      money_line.remove(item)

但由于某种原因,它无法正常工作。它在 money_line 列表中保留了错误的项目。

我在这个 for 循环中遗漏了什么,还是有更好的方法来完成这项工作?

想要的结果:

money_line = [
            {
              "id": 3,
              "book": "BET_RIVERS_VA"
            },
            {
              "id": 4,
              "book": "WILLIAM_HILL"
            {
              "id": 5,
              "book": "SUGAR_HOUSE_NJ"
            }
]

【问题讨论】:

  • 嘿,当我运行你的代码时,“SPORT_888”并没有从结果列表中排除
  • 您的第一个代码示例为我提供了项目 ID 1、4、5、6 的四个字典。这对我来说似乎是正确的。
  • 好的,我编辑了我的示例以满足我的需要。现在会更有意义
  • @C.S.F.Junior 所以你想在 'WYNN_NY' 中保留 'NY' 部分?
  • 是的,我需要保留它。我需要像 for 循环一样比较“WYNN_NY”中的“WYNN”。但由于某种原因无法正常工作。

标签: python list dictionary


【解决方案1】:

你可以同时使用startswith()any()来过滤掉你不想要的字典:

money_line = [
            {
              "id": 1,
              "book": "SPORT_888"
            },
            {
              "id": 2,
              "book": "WYNN"
            },
            {
              "id": 3,
              "book": "BET_RIVERS_VA"
            },
            {
              "id": 4,
              "book": "WILLIAM_HILL"
            },
            {
              "id": 5,
              "book": "SUGAR_HOUSE_NJ"
            },
            {
              "id": 6,
              "book": "WYNN_NY"
            }
]


list_to_remove = ["SPORT_888", "WYNN", "MGM"]

[d for d in money_line if not any(d['book'].startswith(token) for token in list_to_remove)]

结果:

[{'id': 3, 'book': 'BET_RIVERS_VA'},
 {'id': 4, 'book': 'WILLIAM_HILL'},
 {'id': 5, 'book': 'SUGAR_HOUSE_NJ'}]

【讨论】:

  • 最重要的是,它使用 list comprehension 创建一个 new list 包含他们想要的字典,而不是尝试删除他们不想要的字典'不希望从原始列表中。
【解决方案2】:

如果您更喜欢理解(新的字典列表):

d = [d for d in money_line if not any(d['book'].startswith(prefix) for prefix in list_to_remove)]

如果您更喜欢使用 remove(就地)的循环:

for i in reversed([i for i, d in enumerate(money_line) 
                   if any(d['book'].startswith(prefix) for prefix in list_to_remove)]):
    del money_line[i]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2017-07-29
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多