【发布时间】:2021-12-21 01:06:52
【问题描述】:
如何将 numpy 数组 a 设置为字典字典中的三个列表集 one, two, three,就像下面的预期输出一样?
import numpy as np
set_names = np.array(['one', 'two', 'three'])
a = np.array([12,4,2,45,6,7,2,4,5,6,12,4])
dictionary = dict(zip(set_names, np.array_split(a, 3)))
预期输出:
{'one': array([12, 45, 2, 6]),
'three': array([4, 6, 4, 12]),
'two': array([2, 7, 5, 4])}
【问题讨论】:
-
set_names应该只是一个列表,而不是一个 numpy 数组。set_names = ['one', 'two', 'three'].
标签: python arrays numpy dictionary format