Say I have an array x
equal to np.array(0 0 0 0 0 0 0 0 1000 0 0 0 0 1000 1000 1000)
假设我的数组x等于np.array(0 0 0 0 0 0 0 0 1000 0 0 0 0 1000 1000 1000)
and I want to turn it into a matrix array([array([0 0 0 0 0 0 0 0]), array([1000]), array([0 0 0 0]), array([1000 1000 1000])])
. How would I do that?
我想把它变成一个矩阵数组([array([0 0 0 0 0 0 0 0]),array([1000]),array([0 0 0 0]),array([1000 1000 1000] )])。我该怎么办?
The boolean conditions would be, if it's a string of0
's, segment it so that it's one array inside the matrix. If it's a string of 1000
's segment it the same way.
布尔条件是,如果它是0的字符串,则将其分段,使其成为矩阵内的一个数组。如果它是一个1000字节的字符串,它的方式相同。
2 个解决方案
#1
1
Here's one approach with np.split
-
这是np.split的一种方法 -
m = x!=0
out = np.split(x,np.flatnonzero(m[1:] != m[:-1])+1)
Sample run -
样品运行 -
In [53]: x = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 1000, 1000, 1000])
In [54]: m = x!=0
In [55]: np.split(x,np.flatnonzero(m[1:] != m[:-1])+1)
Out[55]:
[array([0, 0, 0, 0, 0, 0, 0, 0]),
array([1000]),
array([0, 0, 0, 0]),
array([1000, 1000, 1000])]
To have an array of arrays as output, wrap it with np.array()
-
要将数组数组作为输出,请使用np.array()包装它 -
In [56]: np.array(np.split(x,np.flatnonzero(m[1:] != m[:-1])+1))
Out[56]:
array([array([0, 0, 0, 0, 0, 0, 0, 0]), array([1000]), array([0, 0, 0, 0]),
array([1000, 1000, 1000])], dtype=object)
For a list of lists as output, here's one way -
对于列表作为输出,这是一种方式 -
m = x!=0
idx = np.r_[0, np.flatnonzero(m[1:] != m[:-1])+1, x.size]
out = [x.tolist()[idx[i]:idx[i+1]] for i in range(len(idx)-1)]
Sample run -
样品运行 -
In [94]: m = x!=0
In [95]: idx = np.r_[0, np.flatnonzero(m[1:] != m[:-1])+1, x.size]
In [96]: [x.tolist()[idx[i]:idx[i+1]] for i in range(len(idx)-1)]
Out[96]: [[0, 0, 0, 0, 0, 0, 0, 0], [1000], [0, 0, 0, 0], [1000, 1000, 1000]]
#2
1
Try this in python:
在python中尝试这个:
a = [0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 1000, 1000, 1000]
out = []
flag = 0
tmp = []
for i in a:
if not flag and i==0:
tmp.append(i)
elif not flag and i==1000:
out.append(tmp)
tmp=[i]
flag=1
elif flag and i==1000:
tmp.append(i)
else:
out.append(tmp)
tmp=[i]
flag=0
out.append(tmp)
print out
#1
1
Here's one approach with np.split
-
这是np.split的一种方法 -
m = x!=0
out = np.split(x,np.flatnonzero(m[1:] != m[:-1])+1)
Sample run -
样品运行 -
In [53]: x = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 1000, 1000, 1000])
In [54]: m = x!=0
In [55]: np.split(x,np.flatnonzero(m[1:] != m[:-1])+1)
Out[55]:
[array([0, 0, 0, 0, 0, 0, 0, 0]),
array([1000]),
array([0, 0, 0, 0]),
array([1000, 1000, 1000])]
To have an array of arrays as output, wrap it with np.array()
-
要将数组数组作为输出,请使用np.array()包装它 -
In [56]: np.array(np.split(x,np.flatnonzero(m[1:] != m[:-1])+1))
Out[56]:
array([array([0, 0, 0, 0, 0, 0, 0, 0]), array([1000]), array([0, 0, 0, 0]),
array([1000, 1000, 1000])], dtype=object)
For a list of lists as output, here's one way -
对于列表作为输出,这是一种方式 -
m = x!=0
idx = np.r_[0, np.flatnonzero(m[1:] != m[:-1])+1, x.size]
out = [x.tolist()[idx[i]:idx[i+1]] for i in range(len(idx)-1)]
Sample run -
样品运行 -
In [94]: m = x!=0
In [95]: idx = np.r_[0, np.flatnonzero(m[1:] != m[:-1])+1, x.size]
In [96]: [x.tolist()[idx[i]:idx[i+1]] for i in range(len(idx)-1)]
Out[96]: [[0, 0, 0, 0, 0, 0, 0, 0], [1000], [0, 0, 0, 0], [1000, 1000, 1000]]
#2
1
Try this in python:
在python中尝试这个:
a = [0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 1000, 1000, 1000]
out = []
flag = 0
tmp = []
for i in a:
if not flag and i==0:
tmp.append(i)
elif not flag and i==1000:
out.append(tmp)
tmp=[i]
flag=1
elif flag and i==1000:
tmp.append(i)
else:
out.append(tmp)
tmp=[i]
flag=0
out.append(tmp)
print out