假设我有一个这样的法令:
exampledict = {0: np.array([1,2,3,4]), 1: np.array([1,2,3])}
我想做的是把它变成一个numpy数组。虽然dict中两个数组的形状不一致,但我想知道,当没有足够的元素时,是否有一种方法可以将其变成一个填充零的2D数组,使其看起来像这样:
[[1、2、3、4]、[1、2、3、0]]
这样做的原因是dict实际上有很多键值对,我希望在后续的计算密集型中避免for循环。
先谢谢你了!
- 亚西尔
我试过了
np.array(list(exampledict.values()))
失败并出现错误ValueError:用序列设置数组元素。请求的数组在1维之后具有不均匀的形状。检测到的形状为(2,)+不均匀部分。
1条答案
按热度按时间s3fp2yjn1#
如果你想使用pandas:
itertools.zip_longest
:输出: