简体   繁体   English

查找列表的最小值、最大值和平均值

[英]Find min, max, and average of a list

I am having hard time to figure out how to find min from a list for example例如,我很难弄清楚如何从列表中找到 min

somelist = [1,12,2,53,23,6,17]

how can I find min and max of this list with defining ( def ) a function如何通过定义( def )函数找到此列表的最小值和最大值

I do not want to use built-in function min我不想使用内置函数min

from __future__ import division

somelist =  [1,12,2,53,23,6,17] 
max_value = max(somelist)
min_value = min(somelist)
avg_value = 0 if len(somelist) == 0 else sum(somelist)/len(somelist)

If you want to manually find the minimum as a function:如果要手动查找最小值作为函数:

somelist =  [1,12,2,53,23,6,17] 

def my_min_function(somelist):
    min_value = None
    for value in somelist:
        if not min_value:
            min_value = value
        elif value < min_value:
            min_value = value
    return min_value

Python 3.4 introduced the statistics package, which provides mean and additional stats: Python 3.4 引入了statistics包,它提供了mean和附加统计信息:

from statistics import mean, median

somelist =  [1,12,2,53,23,6,17]
avg_value = mean(somelist)
median_value = median(somelist)

Return min and max value in tuple:返回元组中的最小值和最大值:

def side_values(num_list):
    results_list = sorted(num_list)
    return results_list[0], results_list[-1]


somelist = side_values([1,12,2,53,23,6,17])
print(somelist)

Only a teacher would ask you to do something silly like this.只有老师会叫你做这种傻事。 You could provide an expected answer.您可以提供预期的答案。 Or a unique solution, while the rest of the class will be (yawn) the same...或者一个独特的解决方案,而班上的其他人将(打哈欠)一样......

from operator import lt, gt
def ultimate (l,op,c=1,u=0):
    try:
        if op(l[c],l[u]): 
            u = c
        c += 1
        return ultimate(l,op,c,u)
    except IndexError:
        return l[u]
def minimum (l):
    return ultimate(l,lt)
def maximum (l):
    return ultimate(l,gt)

The solution is simple.解决方法很简单。 Use this to set yourself apart from obvious choices.使用它使自己与明显的选择区分开来。

list=[]
n = int(input("Enter the length of your list: "))
for i in range (1,n+1):
    a=int(input("Enter the %d number: " %i ))
    list.append(a)
min=list[0]
max=list[0]
for i in range(1,n):
    if max<list[i]:
        max=list[i]
    if min>list[i]:
        min=list[i]
print(" %d is the biggest number " %max)
print(" %d is the smallest number " %min)

Given your list : somelist = [1,12,2,53,23,6,17] First, make it a DataFrame using Pandas.给定您的列表: somelist = [1,12,2,53,23,6,17] 首先,使用 Pandas 将其设为 DataFrame。
Then you can use the describe() method on the DF.然后您可以在 DF 上使用 describe() 方法。 Code:代码:

import pandas as pd
somelist = [1,12,2,53,23,6,17]
somelist_df = pd.DataFrame(somelist)
somelist_df.describe()

OUTPUT:输出:

count   7.000000
mean    16.285714
std 18.052833
min 1.000000
25% 4.000000
50% 12.000000
75% 20.000000
max 53.000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python在列表中的元组中查找最大值,平均值,最小值和相加值 - Python Find max, average, min, and add up values in a tuple in a list 查找字典中两个字段的最小值,最大值和平均值 - Find Min, Max and Average of two fields in dictionary 在 Python Pandas 中查找 ID 的最小值、最大值和平均值 - Find min, max and average of an ID in Python Pandas 列表中的Python平均,最大,最小 - Python Average, Max, Min from list 在txt文件中查找列表的最小,最大和平均值 - Finding the min, max and average of a list in a txt file 如何使用熊猫找到30分钟的平均流量,然后找到每天最多30分钟的平均流量? - How to use Pandas to find 30 min average flows and then find max 30 min average flow per day? 在列表中找到每个嵌套列表的最小值和最大值 - Find the min and max of each nested list in a list 需要在用户输入列表中查找最小值、最大值、范围和平均值,但我无法使用内置函数来查找它们 - Need to find min, max, range, and average in a list of user inputs but I can't use the built in functions to find them 如何使用列中的数据来查找平均值、最大值、最小值等? - How to use data in a column to find average, max, min, etc? 合并列表中的字典以计算Python中的最小值,最大值和平均值 - Merge Dicts in List calculating min, max and average in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM