【问题标题】:How to find average, Max and largest(similar to excel function) in a list in python?如何在python的列表中找到平均值、最大值和最大值(类似于excel函数)?
【发布时间】:2020-09-29 12:12:36
【问题描述】:

我有一个数字列表,我想从这个列表中再创建 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 次。那么任何人都可以帮助回答这个问题吗?

【问题讨论】:

  • 您期待数据帧输出吗?
  • 没有。只是在一个列表或一个 NumPy 数组中,因为我想用它来做进一步的计算。
  • 但您的预期输出看起来像一个数据框。你能修改预期的输出吗?
  • 是的,我知道它看起来像一个 DataFrame,但这只是为了更好的视觉效果。
  • 您的输出示例也毫无意义。制作一个看起来像你想要的东西怎么样..我们可以猜测你想要什么..但这里的想法不是猜测..

标签: python arrays python-3.x list


【解决方案1】:

pandas.DataFrame.explode怎么样

import pandas as pd
df = pd.DataFrame({
    'OvIdx'       : 3 * [range(4)],
    'Average'     : average,
    'Max'         : max,  # should be renamed/assigned as max_ instead
    'Largest(5th)': largest
}).explode('OvIdx').set_index('OvIdx').astype(int)
print(df)

显示

       Average  Max  Largest(5th)
OvIdx                            
0           24   45            14
1           24   45            14
2           24   45            14
3           24   45            14
0           36   76            23
1           36   76            23
2           36   76            23
3           36   76            23
0           34   54            22
1           34   54            22
2           34   54            22
3           34   54            22

从这里开始,您仍然可以执行所有您想要的计算和/或获取 NumPy 数组,执行 df.values


根据您的评论,您还可以将您的列作为单独的实体,例如
>>> df.Average.tolist()
[24, 24, 24, 24, 36, 36, 36, 36, 34, 34, 34, 34]

>>> df.Max.tolist()
[45, 45, 45, 45, 76, 76, 76, 76, 54, 54, 54, 54]

>>> df['Largest(5th)'].tolist()  # as string key since the name is a little bit exotic
[14, 14, 14, 14, 23, 23, 23, 23, 22, 22, 22, 22]

哪种方法开始有点矫枉过正,但可读性强。

【讨论】:

  • @nipunvats 现在好点了吗?
  • 非常感谢。你只是激励我更多地了解熊猫。非常感谢
  • 当然,你真的应该看看 Pandas。在 Python 中,它是数据科学家需要掌握的库。
【解决方案2】:

返回您指定的列表的解决方案

import itertools
import numpy as np

n_times = 4

overdraw = [[16,13,23,14,33,45],[23,11,54,34,23,76],[22,54,34,43,41,11]]
y = [sorted(block, reverse=True) for block in overdraw]

maximum = list(itertools.chain(*[[max(x)]*n_times for x in y]))
average = list(itertools.chain(*[[int(round(sum(x)/len(x)))]*n_times for x in y]))
fifth_largest = list(itertools.chain(*[[x[4]]*n_times for x in y]))

print(f"Average = {average}")
print(f"Max = {maximum}")
print(f"Largest(5th): {fifth_largest}")

输出:

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]

【讨论】:

  • 非常感谢,这实际上正是我想要的答案。谢谢
猜你喜欢
  • 2015-01-16
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 2017-03-08
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多