将 for 循环转换为具有多个词典的列表理解

Convert for loop to list comprehension having multiple dictionaries

排名

[{'driver': 'Max Verstappen', 'team': 'Red Bull', 'home_country': 'Netherlands', 'points': 0}, {'driver': 'Lewis Hamilton', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'George Russell', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Sergio Perez', 'team': 'Red Bull', 'home_country': 'Mexico', 'points': 0}, {'driver': 'Lando Norris', 'team': 'McLaren', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Charles Leclerc', 'team': 'Ferrari', 'home_country': 'Monaco', 'points': 0}, {'driver': 'Valtteri Bottas', 'team': 'Ferrari', 'home_country': 'Spain', 'points': 0}]

driver_with_points

{'Carlos Sainz': 18, 'Charles Leclerc': 26, 'Esteban Ocon': 6, 'Fernando Alonso': 2, 'George Russell': 12, 'Kevin Magnussen': 10, 'Lewis Hamilton': 15, 'Valtteri Bottas': 8, 'Yuki Tsunoda': 4, 'Zhou Guanyu': 1}

如果 driver_with_points 字典中存在车手姓名,我必须根据 driver_with_points 字典中车手的分数更新积分榜。

只需以下几行即可完成任务。

for driver in standings:
    if driver["driver"] in drivers_with_points.keys():
        driver["points"] = drivers_with_points[driver["driver"]]
    

但我需要使用列表理解来完成类似的任务。

谁能帮我把这个 for 循环转换成列表理解?

我试过了

[driver["points"]=drivers_with_points[driver["driver"]] for driver in standings if driver["driver"] in drivers_with_points.keys()]

但是错误Unresolved reference 'drivers_with_points

你不能在理解中做作业 (A=B)。您可以使用更新功能。例如:

[driver.update({'points': drivers_with_points[driver["driver"]]}) for driver in standings if driver["driver"] in drivers_with_points.keys()]

使用推导式执行此操作并没有错,但您不应以更新现有数据结构为目标。通过理解,您应该制作一个 new 列表而不是改变原始列表。理解来自函数式编程,其中不可变数据是目标。

您可以使用列表理解来创建一个 新列表,然后您可以将其分配给 standings。一种方法是解压缩原始 dict 并用 driver_with_points 中的值替换点,如果找不到则用原始值替换点。 dict.get(key, some_default) 是一个很好的模式。

考虑:

standings = [{'driver': 'Max Verstappen', 'team': 'Red Bull', 'home_country': 'Netherlands', 'points': 0}, {'driver': 'Lewis Hamilton', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'George Russell', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Sergio Perez', 'team': 'Red Bull', 'home_country': 'Mexico', 'points': 0}, {'driver': 'Lando Norris', 'team': 'McLaren', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Charles Leclerc', 'team': 'Ferrari', 'home_country': 'Monaco', 'points': 0}, {'driver': 'Valtteri Bottas', 'team': 'Ferrari', 'home_country': 'Spain', 'points': 0}]
drivers_with_points = {'Carlos Sainz': 18, 'Charles Leclerc': 26, 'Esteban Ocon': 6, 'Fernando Alonso': 2, 'George Russell': 12, 'Kevin Magnussen': 10, 'Lewis Hamilton': 15, 'Valtteri Bottas': 8, 'Yuki Tsunoda': 4, 'Zhou Guanyu': 1}

# make a new list with new dicts:
standings = [{**s, 'points': drivers_with_points.get(s['driver'], s['points'])} for s in standings]

这将为您提供一个新列表并将其分配 standings 为:

[{'driver': 'Max Verstappen',
  'team': 'Red Bull',
  'home_country': 'Netherlands',
  'points': 0},
 {'driver': 'Lewis Hamilton',
  'team': 'Mercedes',
  'home_country': 'United Kingdom',
  'points': 15},
 {'driver': 'George Russell',
  'team': 'Mercedes',
  'home_country': 'United Kingdom',
  'points': 12},
 {'driver': 'Sergio Perez',
  'team': 'Red Bull',
  'home_country': 'Mexico',
  'points': 0},
 {'driver': 'Lando Norris',
  'team': 'McLaren',
  'home_country': 'United Kingdom',
  'points': 0},
 {'driver': 'Charles Leclerc',
  'team': 'Ferrari',
  'home_country': 'Monaco',
  'points': 26},
 {'driver': 'Valtteri Bottas',
  'team': 'Ferrari',
  'home_country': 'Spain',
  'points': 8}]