我有一个数字列表,我想从这个列表中再创建 3 个列表,其中包含其中的最大值、平均值和第五大数字。我的原始列表overdraw是列表块,这意味着其中有子块,每个块中有 6 个数字,总共有 3 个块或 6x3 矩阵或数组。
overdraw:
[[16,13,23,14,33,45],[23,11,54,34,23,76],[22,54,34,43,41,11]]
我知道如何计算此列表中的最大值、平均值和 5 个最大值。但是我想要一个特定的答案,比如我知道每个块的最大值、平均值和第 5 个最大值,但我希望它们被打印 4 次。我知道所有的价值观:
Max = [45, 76, 54]
Average = [24, 37, 34]
Largest(5th) = [14, 23, 22]
我的方法:
overdraw = [[16,13,23,14,33,45],[23,11,54,34,23,76],[22,54,34,43,41,11]]
x = [sorted(block, reverse=True) for block in overdraw] # first sort the whole list
max = [x[i][0] for i in range(0, len(x))] # for max
largest = [x[i][4] for i in range(0, len(x))] #5th largest
average = [sum(x[i])/len(x[i]) for i in range(0, len(x))] #average
print("max: ", max)
print("5th largest: ", largest)
print("average: ", average)
运行这段代码后你会得到相同的输出,但我想要这种格式的输出:
Average = [24, 24, 24, 24, 37, 37, 37, 37, 34, 34, 34, 34]
Max = [45, 45, 45, 45, 76, 76, 76, 76, 54, 54, 54, 54]
Largest(5th) = [14, 14, 14, 14, 23, 23, 23, 23, 22, 22, 22, 22]
如您所见,每个平均值、最大值和最大数在各自的列表中打印了 4 次。那么任何人都可以帮助这个答案吗?
慕哥9229398
梵蒂冈之花
相关分类