a:
[array([[0.10865657, 0.10638294, 0.10471012, 0.09508586, 0.09283491],
[0.10892282, 0.10664408, 0.10496752, 0.09531553, 0.09305617],
[0.11664 , 0.1143077 , 0.11259081, 0.1026154 , 0.10025029],
[0.11626453, 0.11392252, 0.11219875, 0.10217754, 0.09980005]]),
array([[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04267657, 0.04255925, 0.04253528, 0.04520177, 0.04655534],
...
I can do a[0].mean
and I will get desired result. By I want to do it to the whole length of the 'a'
with for loop.
I have tried:
mean_all = []
for i in len(dist):
mean = dist[i].mean
mean_all.append(mean)
TypeError: 'int' object is not iterable
First of all, dist[0].mean
returns a function and NOT the mean. You need, in general, dist[0].mean()
.
You can avoid the for loop easily using list comprehension:
from numpy import array
dist = [array([[0.10865657, 0.10638294, 0.10471012, 0.09508586, 0.09283491],
[0.10892282, 0.10664408, 0.10496752, 0.09531553, 0.09305617],
[0.11664 , 0.1143077 , 0.11259081, 0.1026154 , 0.10025029],
[0.11626453, 0.11392252, 0.11219875, 0.10217754, 0.09980005]]),
array([[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04267657, 0.04255925, 0.04253528, 0.04520177, 0.04655534]])]
mean_all = [dist[i].mean() for i in range(len(dist))]
print(mean_all)
[0.10536720549999998, 0.04307523133333334]
If you really want to use the for
loop, use this:
mean_all = []
for i in range(len(dist)):
mean = dist[i].mean()
mean_all.append(mean)
print(mean_all)
[0.10536720549999998, 0.04307523133333334]
使用使用range()的正确格式
for i in range(len(dist))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.