我有一个for
循环,它提供以下输出。
0.53125
0.4375
0.546875
0.578125
0.75
0.734375
0.640625
0.53125
0.515625
0.828125
0.5
0.484375
0.59375
0.59375
0.734375
0.71875
0.609375
0.484375
.
.
.
如何找到前9个值、后9个值等的平均值,并将它们存储到类似于
[0.58,0.20,...]
的列表中?我试过很多东西,但价值观似乎不正确。正确的做法是什么?我所做的:
matchedRatioList = []
matchedRatio = 0
i = 0
for feature in range(90):
featureToCompare = featuresList[feature]
number = labelsList[feature]
match = difflib.SequenceMatcher(None,featureToCompare,imagePixList)
matchingRatio = match.ratio()
print(matchingRatio)
matchedRatio += matchingRatio
if i == 8:
matchedRatioList.append(matchedRatio / 9)
i = 0
matchedRatio = 0
i += 1
最佳答案
一旦你有了数字列表,你就可以使用列表理解来计算每组9个数字的平均值:
from statistics import mean
numbers = [0.53125, 0.4375, 0.546875, 0.578125, 0.75, 0.734375, 0.640625,
0.53125, 0.515625, 0.828125, 0.5, 0.484375, 0.59375, 0.59375,
0.734375, 0.71875, 0.609375, 0.484375]
group_len = 9
matched_ratios = [mean(group) for group in [numbers[i:i+group_len]
for i in range(0, len(numbers), group_len)]]
print(matched_ratios)
# [0.5850694444444444, 0.6163194444444444]
关于python - 查找前9个数字的均值,然后是后9个数字的均值,依此类推,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41344076/