[英]Need to convert 3d array to 2d with mean of inner array
I am receiving a output like the below: 我收到如下输出:
y = [[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
the final output should be as below: 最终输出应如下所示:
Y = [[1, 2, 3], [4, 5, 6]]
How to convert y to Y? 如何将y转换为Y? [Here, the mean of the innermost array (or optionally any element) need to be used as a basic element and the final array (Y) need to be 2D, whereas the pre-final array (y) is 3D.
[这里,最里面的数组(或可选地,任何元素)的均值必须用作基本元素,而最终数组(Y)必须为2D,而最终数组(y)为3D。
(I am new to numpy/python, so this question my look stupid, if any other information is needed, please let me know) (我是numpy / python的新手,所以这个问题我看起来很愚蠢,如果需要其他信息,请告诉我)
Try this, 尝试这个,
result = []
for i in lst:
result.append([j[0] for j in i])
print result
Result 结果
[[1, 2, 3], [4, 5, 6]]
With list comprehension: 使用列表理解:
y = [[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
Y = [[x[0] for x in z] for z in y] # [[1, 2, 3], [4, 5, 6]]
import numpy as np
y = [[[1, 1, 1, 1, 1, 1, 1 ], [2, 2, 2, 2, 2, 2, 2 ] ,[3, 3, 3, 3, 3, 3, 3]],[[4, 4, 4],[5, 5, 5],[6, 6, 6]]]
Y = []
for l in y:
inner_l = []
for sub_l in l :
inner_l.append(np.mean(sub_l))
Y.append(inner_l)
print Y
y=[[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
rslt = []
for x in y:
lst=[]
for m in x:
lst.append([m[0]])
rslt.append(lst)
Result: 结果:
rslt = [[1, 2, 3], [4, 5, 6]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.