python - python 中的 4 维零数组

标签 python arrays numpy

我想在 python 中制作一个 4 维零数组。 我知道如何为方形数组执行此操作,但我希望列表具有不同的长度。

现在我用这个:

numpy.zeros((200,)*4)

它们的长度均为 200,但我希望长度为 200,20,100,20,因为现在我的数组中有很多我不使用的零

最佳答案

你可以使用np.full:

>>> np.full((200,20,10,20), 0)

numpy.full

Return a new array of given shape and type, filled with fill_value.

示例:

>>> np.full((1,3,2,4), 0)
array([[[[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]],

        [[ 0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.]]]])

关于python - python 中的 4 维零数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921407/

相关文章:

python - 查找两个数据帧之间的交集并取平均值 - Python

python - Pyspark DataFrame - 如何将一列从分类值转换为 int?

python - __contains__ 做什么,什么可以调用 __contains__ 函数

javascript - 如何通过检查 JavaScript 中的属性值来查找数组中对象的索引?

javascript - 为用例组合数组

python - 如何将 int numpy 数组的列乘以 float 数字并保持为 int?

python - 如何让我的 wxpython 顶部框架显示在我的桌面中间?

python - 使用列表中的掩码值掩蔽 python 二维数组

python - 在numpy中过滤掉具有零值的列

python - 使用类型为 "object"的 numpy 数组创建混合类型的 Pandas Dataframe