小编tha*_*y33的帖子

如何在Python中将字典的键值对收集到一个大字典中?

我有一本以下格式的字典:

data = {
    'Bob': {
        'age': 12,
        'weight': 150,
        'eye_color': 'blue'
    },
    'Jim': {
        'favorite_food': 'cherries',
        'sport': 'baseball',
        'hobby': 'running'
    },
    'Tom': {
        'strength': 'average',
        'endurance': 'high',
        'heart_rate': 'low'
    }
}
Run Code Online (Sandbox Code Playgroud)

将 dict 中的所有字典连接成一个新字典的最 Pythonic 方法是什么,这样我最终会得到如下所示的结果:

new_dict = {
    'age': 12,
    'weight': 150,
    'eye_color': 'blue',
    'favorite_food': 'cherries',
    'sport': 'baseball',
    'hobby': 'running',
    'strength': 'average',
    'endurance': 'high',
    'heart_rate': 'low'
}
Run Code Online (Sandbox Code Playgroud)

python dictionary

2
推荐指数
1
解决办法
68
查看次数

标签 统计

dictionary ×1

python ×1