将字典列表转换为列表的字典



我有一个键相同的字典列表。此外,每个字典只有两个"键:值"对。我想将字典列表转换为具有两个键的单个字典,每个键表示一个列表值。使得相同键的值驻留在一个列表中。

我想出了下面的方法,顺便说一下,它工作得很好。但是我想知道是否有更好的实用方法。

字典列表:

my_list = [{'old_price': None, 'price': '5,75'}, {'old_price': None, 'price': '5,90'}, {'old_price': None, 'price': '5,95'}, {'old_price': None, 'price': '10,15'}, {'old_price': None, 'price': '19,90'}, {'old_price': None, 'price': '34,90'}, {'old_price': None, 'price': '46,50'}, {'old_price': None, 'price': '24,90'}]

List of Dictionaries to Dictionary of Lists:

single_dict = {"old_price": [item['old_price'] for item in my_list], "price":[item['price'] for item in my_list]}

结果字典:

{'old_price': [None, None, None, None, None, None, None, None], 'price': ['5,75', '5,90', '5,95', '10,15', '19,90', '34,90', '46,50', '24,90']}

使用defaultdict,可以使算法0 (N),单次遍历列表。

>>> my_list = [{'old_price': None, 'price': '5,75'}, {'old_price': None, 'price': '5,90'}, {'old_price': None, 'price': '5,95'}, {'old_price': None, 'price': '10,15'}, {'old_price': None, 'price': '19,90'}, {'old_price': None, 'price': '34,90'}, {'old_price': None, 'price': '46,50'}, {'old_price': None, 'price': '24,90'}]
>>> from collections import defaultdict
>>> d = defaultdict(list)  # will create a list for every missing entry
>>> for adict in my_list:
...    for key, value in adict.items():
...       d[key].append(value)
...    
>>> d
defaultdict(<class 'list'>, {'old_price': [None, None, None, None, None, None, None, None], 'price': ['5,75', '5,90', '5,95', '10,15', '19,90', '34,90', '46,50', '24,90']})

如果你事先知道你需要的键,你可以用固定的分配交换循环(但是时间成本非常小):

>>> for adict in my_list:
...    d["old_price"].append(adict["old_price"])
...    d["price"].append(adict["price"])
...

迭代到您的dict,然后创建一个新的字典,检查是否已经创建。

my_list = [{'old_price': None, 'price': '5,75'}, {'old_price': None, 'price': '5,90'}, {'old_price': None, 'price': '5,95'}, {'old_price': None, 'price': '10,15'}, {'old_price': None, 'price': '19,90'}, {'old_price': None, 'price': '34,90'}, {'old_price': None, 'price': '46,50'}, {'old_price': None, 'price': '24,90'}]
result = {}
for each_dict in my_list:
for k in each_dict:
if (k in result): 
result[k].append(each_dict[k])
else:
result[k] = [each_dict[k]]
print(result)

输出:

{'old_price': [None, None, None, None, None, None, None, None], 'price': ['5,75', '5,90', '5,95', '10,15', '19,90', '34,90', '46,50', '24,90']}

处理数据,我习惯使用pandas…

import pandas as pd
my_list = [{'old_price': None, 'price': '5,75'}, {'old_price': None, 'price': '5,90'},
{'old_price': None, 'price': '5,95'}, {'old_price': None, 'price': '10,15'},
{'old_price': None, 'price': '19,90'}, {'old_price': None, 'price': '34,90'},
{'old_price': None, 'price': '46,50'}, {'old_price': None, 'price': '24,90'}]
df = pd.DataFrame(my_list)
columns = df.columns.tolist()
data = {}
// merge same key
for key in columns:
data[key] = df[key].tolist()
print(data)

试试这个:

values = list(map(list, zip(*[item.values() for item in my_list])))
single_dict = { 'old_price':values[0], 'price':values[1] }

相关内容

  • 没有找到相关文章

最新更新