【问题标题】:Exchange the items of list with values of a dictionary Python将列表项与字典 Python 的值交换
【发布时间】:2015-10-18 17:49:17
【问题描述】:

我有一个这样的字典列表

data = [
    {
        "B3": 0.9500000000000000
    },
    {
        "C3": 0.9000000000000000
    },
    {
        "D3": 0.8900000000000000
    },
    {
        "E3": 0.8800000000000000
    },
    {
        "F3": 0.8700000000000000
    },
    {
        "G3": 0.8600000000000000
    },
    {
        "H3": 0.8500000000000000
    },
    {
        "I3": 0
    },
    {
        "J3": 0
    },
    {
        "K3": 0
    },
    {
        "L3": 0
    },
    {
        "M3": 0
    }
]

和这样的驱动字典

driver = {
    1: [600.0, 625.0, 700.0, 650.0, 660.0, 800.0, 675.0, 650.0, 600.0, 700.0, 690.0, 750.0],
    2: [580.0, 607.0, 685.0, 626.0, 640.0, 770.0, 665.0, 639.0, 595.0, 665.0, 675.0],
    3: [560.0, 589.0, 670.0, 602.0, 620.0, 740.0, 655.0, 628.0, 590.0, 630.0],
    4: [540.0, 571.0, 655.0, 578.0, 600.0, 710.0, 645.0, 617.0, 585.0],
    5: [520.0, 553.0, 640.0, 554.0, 580.0, 680.0, 635.0, 606.0],
    6: [500.0, 535.0, 625.0, 530.0, 560.0, 650.0, 625.0],
    7: [480.0, 517.0, 610.0, 506.0, 540.0, 620.0],
    8: [460.0, 499.0, 595.0, 482.0, 520.0],
    9: [440.0, 481.0, 580.0, 458.0],
    10: [420.0, 463.0, 565.0],
    11: [400.0, 445.0],
    12: [380.0]
}

我需要将列表中每个数据字典的键与驱动程序字典的值进行映射。

示例输出如下所示

{
    'B3': [600.0, 625.0, 700.0, 650.0, 660.0, 800.0, 675.0, 650.0, 600.0, 700.0, 690.0, 750.0],
    'C3': [580.0, 607.0, 685.0, 626.0, 640.0, 770.0, 665.0, 639.0, 595.0, 665.0, 675.0]
}

等等。

我的方法是首先将字典列表转换为键列表,然后遍历驱动程序字典中的每个项目并弹出该项目并用新列表中的键替换它。

from collections import Iterable

k_list = []
for item in data:
    k_list.append(list(item.keys()))

print(k_list)


def flatten(items, ignore_types=(str, bytes)):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, ignore_types):
            yield from flatten(x)
        else:
            yield x


x = list(flatten(k_list))
print(x)

nn = {}
for k, v in driver.items():
    for i in x:
        nn[i] = v

print(nn)

这给了我这样的输出

{'L3': [380.0], 'C3': [380.0], 'D3': [380.0], 'E3': [380.0], 'G3': [380.0], 'M3': [380.0], 'B3': [380.0], 'I3': [380.0], 'F3': [380.0], 'H3': [380.0], 'K3': [380.0], 'J3': [380.0]}

我哪里做错了?

注意:排序很重要,即数据字典的B3 应该与驱动字典的1 匹配,依此类推。

【问题讨论】:

  • 驱动程序字典中的键是否总是从 1 到 n 的数字并有序?
  • {next(iter(data_item)): driver[driver_key] for data_item, driver_key in zip(data, sorted(driver))}
  • @tobias_k 是的,它们总是有序的。
  • {next(iter(data_item)): driver[i] for i, data_item in enumerate(data, 1)} 如果drivers 的键总是1n

标签: python python-3.x dictionary


【解决方案1】:

首先,您应该使用sorted 而不是只使用driver.items() 以确保项目的顺序正确(它们应该无论如何都应该排序,但不能保证这一点)。然后,您应该zip 键和驱动程序数据,而不是使用嵌套循环。最后,为了确保生成的 dict 是有序的(如果这很重要),请使用 OrderedDict

k_list = [k for item in data for k in item.keys()]
nn = collections.OrderedDict()
for k, (n, v) in zip(k_list, sorted(driver.items())):
    nn[k] = v

您的嵌套循环方法的问题在于,您用最后一次迭代中的最后一个值覆盖了所有键

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多