如何基于 Python 中的公共键值对有效地将键值从一个字典列表插入到另一个字典?

时间:2021-05-12 04:01:19

标签: python list dictionary optimization

我有一个很大的 Python 字典列表。示例如下:

big_list_dictionary = [{
    'name': 'test = 1',
    'id': 1,
    'value': 30
},{
    'name': 'apple = 1',
    'id': 2,
    'value': 70
},{
    'name': 'orange = 1',
    'id': 3,
    'value': 10
},{
    'name': 'balloon = 1',
    'id': 4,
    'value': 20
},{
    'name': 'airplane = 1',
    'id': 5,
    'value': 40
}]

我有两个字典及其总值的列表

total1 = [{
    'name': 'test',
    'total': 130
},{
    'name': 'apple',
    'total': 270
},{
    'name': 'orange',
    'total': 310
},{
    'name': 'balloon',
    'total': 420
},{
    'name': 'airplane',
    'total': 540
}]

total2 = [{
    'name': 'test',
    'total': 230
},{
    'name': 'apple',
    'total': 570
},{
    'name': 'orange',
    'total': 3210
},{
    'name': 'balloon',
    'total': 620
},{
    'name': 'airplane',
    'total': 940
}]

如果您注意到,nametotal1 中的 total2 与省略 big_list_dictionary= 1 略有不同。

如何将 total1total2 的总值添加到 big_list_dictionary 中,以便最终结果如下所示:

[{
    'name': 'test = 1',
    'id': 1,
    'value': 30,
    'total2': 230,
    'total1': 130
},{
    'name': 'apple = 1',
    'id': 2,
    'value': 70,
    'total2': 570,
    'total1': 270
},{
    'name': 'orange = 1',
    'id': 3,
    'value': 10,
    'total2': 3210,
    'total1': 310
},{
    'name': 'balloon = 1',
    'id': 4,
    'value': 20,
    'total2': 620,
    'total1': 420
},{
    'name': 'airplane = 1',
    'id': 5,
    'value': 40,
    'total2': 940,
    'total1': 540
}]

目前,我做的方式很慢。

    for item in big_list_dictionary:
        for t1,t2 in zip(total1,total2):
            if t1['name'] in item['name']:
                item['total1] = t1['total']
                item['total2'] = t2['total']

我怎样才能有效地做到这一点?

1 个答案:

答案 0 :(得分:2)

如果额外的字符总是 =1,那么您可以创建一个中间关系,然后使用以下代码。

big_list_dictionary = [{'name': 'test = 1','id': 1,'value': 30},{'name': 'apple = 1','id': 2,'value': 70},{'name': 'orange = 1','id': 3,'value': 10},{'name': 'balloon = 1','id': 4,'value': 20},{'name': 'airplane = 1','id': 5,'value': 40}]

total1 = [{ 'name': 'test','total': 130},{'name': 'apple','total': 270},
{'name': 'orange','total': 310},{'name': 'balloon','total': 420},{'name': 'airplane','total': 540}]
total2 = [{'name': 'test','total': 230},{'name': 'apple','total': 570},{'name': 'orange','total': 3210},{'name': 'balloon','total': 620},{'name': 'airplane','total': 940}]


intermediate = {i['name'].split('=')[0].strip():i for i in big_list_dictionary}

for t1, t2 in zip(total1, total2):
    intermediate[t1['name']]['total1'] = t1['total']
    intermediate[t1['name']]['total2'] = t2['total']

print(big_list_dictionary)

输出

[{'name': 'test = 1', 'id': 1, 'value': 30, 'total1': 130, 'total2': 230},
 {'name': 'apple = 1', 'id': 2, 'value': 70, 'total1': 270, 'total2': 570},
 {'name': 'orange = 1', 'id': 3, 'value': 10, 'total1': 310, 'total2': 3210},
 {'name': 'balloon = 1', 'id': 4, 'value': 20, 'total1': 420, 'total2': 620},
 {'name': 'airplane = 1', 'id': 5, 'value': 40, 'total1': 540, 'total2': 940}]

基准

%%timeit -n10 -r10 的长度分别为 big_list_dictionarytotal1total2。增加了长度以显示效率。

这个解决方案

1000

您的解决方案

513 µs ± 17.2 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)

此解决方案更快。 (91.6 ms ± 1.19 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)) 两种解决方案之间的差异只会随着长度的增加而增加。

根据评论进行编辑:

我相信 513 µs < 91.6 mstest1 中有一些元素不在 test2,因为假设 big_list_dictionarytest1 具有相同顺序的相同元素,您可以只遍历列表之一,如果未找到则追加到 {{1 }} 并将其添加到 test2。这会在 big_list_dictionary 的末尾附加所有新字典,在末尾添加比在随机位置插入更快。但是,如果您确实在意订单,那么我相信您的解决方案已尽善尽美。

免责声明:我没有测试这部分代码,因为我没有输入或输出来检查所需的行为。

intermediate
相关问题