我很难找出如何从 list 中找到分钟
例如
somelist = [1,12,2,53,23,6,17]
如何通过定义(def)函数来查找此列表的最小值和最大值

我不想使用内置函数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)

如果要手动查找最小值作为函数:
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引入了 statistics 软件包,该软件包提供mean和其他统计信息:
from statistics import mean, median

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

关于python - Python查找列表的最小最大值和平均值(数组),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27009247/

10-11 04:37