如何在字典列表中按键优化列表中的值分组?



下面的脚本工作,但我想知道是否有一个更快的解决方案?对于非常大的字典列表,我注意到有一点延迟。

from collections import defaultdict
input = [{"first": 1.56,
"second": [1, 2, 3, 4]}, {"first": 7.786,
"second": [5, 6, 7, 8]}, {"first": 4.4,
"second": [9, 10, 11, 12]}]
output = [{"first": [1.56, 7.786, 4.4],
"second":[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]}]
my_dictionary = defaultdict(list)
for item in input:
for key, value in item.items():
my_dictionary[key].append(value)
print(my_dictionary)
#defaultdict(<class 'list'>, {'first': [1.56, 7.786, 4.4], 'second': [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]})

似乎字典中的键是相同的,所以您可以使用字典理解:

out = {k:[d[k] for d in input] for k in input[0]}

另一个非常快速的替代方法是使用cytoolz模块。

# pip install cytoolz
from cytoolz.dicttoolz import merge_with
out = merge_with(list, *input)

输出:

{'first': [1.56, 7.786, 4.4],
'second': [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]}

时间:

>>> my_input = input * 10000
>>> %%timeit
... my_dictionary = defaultdict(list)
... for item in my_input:
...     for key, value in item.items():
...         my_dictionary[key].append(value)
20.3 ms ± 2.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> %timeit out = {k:[d[k] for d in my_input] for k in my_input[0]}
4.65 ms ± 541 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
>>> %timeit out = merge_with(list, *my_input)
5.58 ms ± 2.09 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

相关内容

  • 没有找到相关文章

最新更新