Python 从numpy数组生成多维空值或空值
我想基于此数组创建一个填充有1或0的数组Python 从numpy数组生成多维空值或空值,python,arrays,numpy,multidimensional-array,Python,Arrays,Numpy,Multidimensional Array,我想基于此数组创建一个填充有1或0的数组 testArray = np.array([7,5,3]) 所以最终的结果应该是 [[1,1,1,1,1,1,1], [1,1,1,1,1], [1,1,1]] 正如@JoshAdel在评论中所说,int数组不能是锯齿状的,这意味着行的长度不能不同。这就是你要找的吗 public class soArray { public static void main(String[] args) { int[][] testArray = so
testArray = np.array([7,5,3])
所以最终的结果应该是
[[1,1,1,1,1,1,1],
[1,1,1,1,1],
[1,1,1]]
正如@JoshAdel在评论中所说,int数组不能是锯齿状的,这意味着行的长度不能不同。这就是你要找的吗
public class soArray {
public static void main(String[] args) {
int[][] testArray = soArray.array(7,5,3);
for (int i = 0; i < testArray.length; i++){
for (int j = 0; j < testArray[0].length; j++){
System.out.print(testArray[i][j]);
}
System.out.println();
}
}
public static int[][] array(int a, int b, int c){
int max;
if(a > b && a > c)
max = a;
else if(b > a && b > c)
max = b;
else
max = c;
int[][] out = new int[3][max];
for (int i = 0; i < max; i++){
if(i < a)
out[0][i] = 1;
else
out[0][i] = 0;
}
for(int i = 0; i< b; i++){
if(i < b)
out[1][i] = 1;
else
out[1][i] = 0;
}
for(int i = 0; i < c; i++){
if(i < c)
out[2][i] = 1;
else
out[2][i] = 0;
}
return out;
}
公共类{
公共静态void main(字符串[]args){
int[]testArray=soArray.array(7,5,3);
对于(int i=0;ib&&a>c)
max=a;
否则如果(b>a&&b>c)
max=b;
其他的
max=c;
int[]out=新int[3][max];
对于(int i=0;i
}
它将打印出:
1111111
1111100
1110000
numpy数组中的每一行(和列等)必须具有相同的长度。您可以实现@ChrisWilson4所做的,并用0
或np.nan
填充空部分。创建一个空数组,行数等于length
,列数等于最大行:
fill = 1 # or `0` or `np.nan`
background = 0 # or `np.nan`
lengths = np.array([7,5,3])
a = np.ones((lengths.size, lengths.max()))*background
然后用您的fill
值填充:
for row, length in enumerate(lengths):
a[row,:length] = fill
a
#array([[ 1., 1., 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1., 0., 0.],
# [ 1., 1., 1., 0., 0., 0., 0.]])
或者,对于fill=0
和background=np.nan
:
array([[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., nan, nan],
[ 0., 0., 0., nan, nan, nan, nan]])
或者,您可以使用纯python方式(不使用numpy)创建列表列表,如下所示:
这将为您提供一个不规则的
对象数组
数据类型:
>>> result = np.array([np.ones(a) for a in testArray])
>>> print result
[[ 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1.] [ 1. 1. 1.]]
对于零,只需使用
np。零
只需写一个快速列表:
>>> holder = [np.ones((testArray[i])) for i in range(len(testArray))]
>>> holder
[array([[ 1., 1., 1., 1., 1., 1., 1.]]), array([[ 1., 1., 1., 1., 1.]]), array([[ 1., 1., 1.]])]
如果希望其采用写入格式,则始终可以对其进行重塑:
>>> np.array(holder).reshape(3,1)
array([[array([ 1., 1., 1., 1., 1., 1., 1.])],
[array([ 1., 1., 1., 1., 1.])],
[array([ 1., 1., 1.])]], dtype=object)
问题解决了 您的最终结果是否应该是一个7x7数组,其中包含零,而您没有指定零?Numpy数组不能有如您所示的锯齿状数据。您计划如何使用或访问最终数组?一个列表列表可能是最好的,但是一个带有填充符的数组或一个数组对象数组可能会更好地工作,这取决于您的用例。(
numpy
是一个Python向量数学库。)哈哈哈,我花了大约20分钟的时间输入它,甚至没有阅读标记。哦,我多么喜欢浪费时间。注意,你不能像普通数组那样对其进行索引,因为它只是列表的numpy
版本,但它是一个数组数组。从技术上讲,你可以将此数组作为普通数组进行索引(例如,你可以有一个(2x2)上面的版本,并按常规进行切片。您不能做的是将单个元素作为数组的一部分进行索引。对象数组比列表有一些优点。我不确定我是否会将其称为不规则数组:它是一个普通数组,形状(3,)
,其行为也是如此。@DSM:按照这种逻辑,C中也没有不规则的数组。你是对的,但这里感兴趣的形状是(3,)
,它的行为不是这样的。如果它是“2x2”,那么它的形状就只是(2,2,)
而且你不能以普通方式访问最后一个轴。我想在知道哪一个更好之前,我们需要从OP获得更多关于它们计划使用的信息。+1建议:)你可以使用numpy对象数组,其中每个元素都是不同大小的数组(参见我的答案)。哇,我甚至没有注意到……这是因为我使用列表理解并在其中生成一个数组,因为它返回一个numpy数组。这意味着列表理解中的每个数组都是2d的:您使用了np.ones((1,testArray[I]))
,为什么不使用np.ones(testArray[I])
?这只是一种习惯,因为我通常只对多维数组使用numpy。固定的。
>>> np.array(holder).reshape(3,1)
array([[array([ 1., 1., 1., 1., 1., 1., 1.])],
[array([ 1., 1., 1., 1., 1.])],
[array([ 1., 1., 1.])]], dtype=object)