python - 通过匹配 python 中的内部 dicts 属性来展平两个字典列表?

标签 python python-2.7 dictionary

背景: 我从后端获取一些数据 (regions =[]),我需要通过匹配向列表中的每个字典添加属性 (order = [])蛞蝓。我将在 jinja 模板过滤器中按此属性进行排序。

在 javascript 中,我会使用函数式数组方法,例如 mapzip 等;不幸的是,我是一个Python菜鸟,我不知道这些方法的等价物是什么。

问题:

如何简洁地将列表order展平为regions并在python中接收预期的输出regions_with_order

Repl

# I made this to zip into regions
order = [
  {'slug': 'north-america', 'order': 1},
  {'slug': 'latin-america', 'order': 2},
  {'slug': 'europe', 'order': 3},
  {'slug': 'asia-pacific', 'order': 4},
  {'slug': 'africa-middle-east', 'order': 5},
]

# I received this from backend cms.   
regions = [
  {'slug': 'africa-middle-east', 'lat': '123'},     
  {'slug': 'europe','lat': '1231'},
  {'slug': 'asia-pacific', 'lat': '1230'},
  {'slug': 'latin-america',  'lat': '1232'},
  {'slug': 'north-america', 'lat': '1234'},
]



# Expected OUTPUT

# regions_with_order = [
#   {'slug': 'north-america', 'lat': '1234',  'order': 1},
#   {'slug': 'latin-america',  'lat': '1232',  'order': 2},
#   {'slug': 'europe','lat': '1231', 'order': 3},
#   {'slug': 'asia-pacific', 'lat': '1230','order': 4},
#   {'slug': 'africa-middle-east', 'lat': '123','order': 5}
# ]

最佳答案

首先将区域列表转换为字典,这样您就可以查找 lat按 slug 划分的每个区域的值,有效:

def merge_dicts(d1, *ds):
    final = d1.copy()
    for d in ds:
        final.update(d)
    return final

regionmap = {r['slug']: r for r in regions}
regions_with_order = [merge_dicts(o, regionmap[o['slug']]) for o in order]

在Python 3中,您可以替换merge_dicts()调用 {**o, **regionmap[o['slug']]} .

这会产生您想要的输出:

>>> from pprint import pprint
>>> order = [
...   {'slug': 'north-america', 'order': 1},
...   {'slug': 'latin-america', 'order': 2},
...   {'slug': 'europe', 'order': 3},
...   {'slug': 'asia-pacific', 'order': 4},
...   {'slug': 'africa-middle-east', 'order': 5},
... ]
>>> regions = [
...   {'slug': 'africa-middle-east', 'lat': '123'},
...   {'slug': 'europe','lat': '1231'},
...   {'slug': 'asia-pacific', 'lat': '1230'},
...   {'slug': 'latin-america',  'lat': '1232'},
...   {'slug': 'north-america', 'lat': '1234'},
... ]
>>> regionmap = {r['slug']: r['lat'] for r in regions}
>>> [merge_dicts(o, regionmap[o['slug']]) for o in order]
[{'slug': 'north-america', 'order': 1, 'lat': '1234'}, {'slug': 'latin-america', 'order': 2, 'lat': '1232'}, {'slug': 'europe', 'order': 3, 'lat': '1231'}, {'slug': 'asia-pacific', 'order': 4, 'lat': '1230'}, {'slug': 'africa-middle-east', 'order': 5, 'lat': '123'}]
>>> pprint(_)
[{'lat': '1234', 'order': 1, 'slug': 'north-america'},
 {'lat': '1232', 'order': 2, 'slug': 'latin-america'},
 {'lat': '1231', 'order': 3, 'slug': 'europe'},
 {'lat': '1230', 'order': 4, 'slug': 'asia-pacific'},
 {'lat': '123', 'order': 5, 'slug': 'africa-middle-east'}]

关于python - 通过匹配 python 中的内部 dicts 属性来展平两个字典列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47821883/

相关文章:

python - 在 Pandas 中添加多列

python - 如何提供 API 来获取 django-rest 中任何模型的更改历史记录?

python - setup.py 缺少 __init__.py 文件

Python:将多个列表转换为字典数组

python - 将字典列表写入 Pandas Dataframe 中的同一行

python - estimator.predict 引发 "ValueError: None values not supported"

python - 在 HDF5 C++ api 中使用 GZIP 压缩时,是否默认启用自动分块?

python - 你如何在 linux 上为 python 2.7 和 python 3.2 设置 virtualenv?

python - IPython 中数组元素的 ID 发生变化

python - 如何获取字典列表中所有人的名字?