import numpy as np
data = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]
grouped_data = {}
for item in data:
for key, value in item.items():
if key in grouped_data:
grouped_data[key].append(value)
else:
grouped_data[key] = [value]
for key in grouped_data:
grouped_data[key] = np.array(grouped_data[key])
for key, value in grouped_data.items():
print(f'"{key}": {value}')
import numpy as np
data = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]
result = {}
for item in data:
key, value = list(item.items())[0]
if key not in result:
result[key] = []
result[key].append(value)
result = {key: np.array(value) for key, value in result.items()}
print(result)
import numpy as np
# your data is a list
data = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]
# make an empty dictionary
converted_data = {}
# loop through all elements in your list
for d in data:
# the elements in the list are dictionaries, through which you loop like this:
for key, value in d.items():
if key in converted_data: # to check if the key is already in there, keys in dictianaries need to be unique
converted_data[key].append(value)
else:
converted_data[key] = [value] # 'converted_data[key]' is the way to get a value based on a key
# convert lists to numpy arrays
for key in converted_data:
converted_data[key] = np.array(converted_data[key])
converted_data
import pandas as pd
import numpy as np
[pd.DataFrame(lst).melt(var_name='var',value_name='val').dropna().groupby('var')['val'].apply(np.array).to_dict()]
In [23]: for d in alist:
...: for k in d:
...: dd[k].append(d[k])
...:
In [24]: dd
Out[24]: defaultdict(list, {'Cool': [128, 51], 'Hot': [75, 62], 'Archive': [144, 12]})
型 它可以被转换为一个数组值的dict:
In [25]: dd1={k:np.array(v) for k,v in dd.items()}
In [26]: dd1
Out[26]:
{'Cool': array([128, 51]),
'Hot': array([75, 62]),
'Archive': array([144, 12])}
import numpy as np
data = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]
result_dict = {}
for d in data:
for key, value in d.items():
if key not in result_dict:
result_dict[key] = []
result_dict[key].append(value)
result_dict_np = {key: np.array(value) for key, value in result_dict.items()}
for key, value in result_dict_np.items():
print(f'"{key}": {value}')
6条答案
按热度按时间vuv7lop31#
试试这个
字符串
ar5n3qh52#
字符串
hsvhsicv3#
你需要在一个循环中加入一个for循环:
字符串
q0qdq0h24#
您可以尝试
pandas
字符串
或者更简洁(感谢@mozway在评论中的建议)
型
你将获得
型
数据
型
pdkcd3nj5#
一个方便的分组工具是
collections.defaultdict
。你定义一个dict,如果找不到键,它会接受一个默认的初始值。这是@mozway建议的setdefault
的简化版本。字符串
然后从列表中填充它。第二个循环让我们在一个dict中有多个键。知道/假设每个dict只有一个键并不会让这更快或更简单
型
它可以被转换为一个数组值的dict:
型
r9f1avp56#
这里有个密码
字符串