将字典列表转换为带分组的NumPy数组

jm81lzqq  于 12个月前  发布在  其他
关注(0)|答案(6)|浏览(88)

请有人可以帮助我,如何优雅地转换这个字典列表:

[{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]

字符串
到这个NumPy数组列表:

"Cool": np.array([128,51])
"Hot": np.array([75,62])
"Archive": np.array([144,12])


谢谢你的任何线索。

vuv7lop3

vuv7lop31#

试试这个

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}')

字符串

ar5n3qh5

ar5n3qh52#

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)

字符串

hsvhsicv

hsvhsicv3#

你需要在一个循环中加入一个for循环:

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

字符串

q0qdq0h2

q0qdq0h24#

您可以尝试pandas

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()]

字符串
或者更简洁(感谢@mozway在评论中的建议)

[pd.DataFrame(lst).stack().groupby(level=1).apply(np.array).to_dict()]


你将获得

[{'Archive': array([144.,  12.]),
  'Cool': array([128.,  51.]),
  'Hot': array([75., 62.])}]

数据

lst = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]

pdkcd3nj

pdkcd3nj5#

一个方便的分组工具是collections.defaultdict。你定义一个dict,如果找不到键,它会接受一个默认的初始值。这是@mozway建议的setdefault的简化版本。

In [19]: alist = [{'Cool': 128}, {'Cool': 51}, {'Hot': 75}, {'Hot': 62}, {'Archive': 144}, {'Archive': 12}]

In [20]: from collections import defaultdict
In [21]: dd = defaultdict(list) # initial
In [22]: dd
Out[22]: defaultdict(list, {})

字符串
然后从列表中填充它。第二个循环让我们在一个dict中有多个键。知道/假设每个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])}

r9f1avp5

r9f1avp56#

这里有个密码

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}')

字符串

相关问题