如何从python列表中计算多个平均值

时间:2012-01-19 22:13:53

标签: python

['2598.95165','2541.220308','221068.0401','end','4834.581952','1056.394859','3010.609563','2421.437603','4619.861889','746.040504','268.3881793','379.3934898 ',''1252.527752','11459.88522','4862.167506','506.924289','634.6737389','496.4679199','17941.59143','919.4998935','7247.610974','1166.053214','47360.91508','855.2426137', '4020.444585','4469.896904','2615.874982','19862.92009','2379.619573','1203.268956','4399.589212','6838.825864','1848.407564','3527.198403',''33976.85042','818.8722263','634.6652078 ',''469.2685928','4864.830004','5103.222941','1011.239929','829.9915382','8571.237936','3301.953656','14594.47385','25688.83822','4024.393045','4163.775185','1775.894366', '3682.012227','3371.092883','6651.509488','7906.092773','7297.133447','end','4566.874299','end','4255.700077','1857.648393','11289.48095','2070.981805','1817.505094 ','1892.256615','1757.0048','59458.46328','778.5755201','54987.32423','2245.172711','722。 2619663','5116.616632','3427.865861','17973.07118','14398.74281','66313.92115','11585.24151','45294.03043','6524.744077','25958.80015','593.3786209','2899.040703','85577.21342' ,'153576.2633','5852.008444','563.0265409','70796.45356','565.2123689','6560.030116','2668.934414','418.666014','5216.392132','760.894589','8072.957639','346.5905371','端']

我想创建一个函数,给定一个bin,它是一个列表(例如上面的例子),生成由字符串'end'分隔的数字的平均值。我希望从上面的bin中得到4个平均值,因为有4组数字由4个'end'字符串分隔

有人可以帮我修改下面看起来不起作用的代码:

def average(bin):
    total = 0.0
    count=0
    for number in bin:       
        if number==float(number):
            total += float(number)
            count+=1
            avg = total/count
        elif number=='end':
            continue

    return avg

4 个答案:

答案 0 :(得分:4)

def averages(bin):
    li, s, n = [], 0, 0
    for item in bin:
        if item == 'end':
            li.append(s / n)
            s, n = 0, 0
        else:
            s += float(item)
            n += 1
    return li

或者,你可以试试这个单行:

    averages = [sum(k) / len(k) for k in [[float(j) for j in i.split() if j] for i in ' '.join(bin).split('end') if i]]

答案 1 :(得分:2)

这是计算这些平均值的一种非常简洁的方法:

def average(bin):
    start = 0
    while 'end' in bin[start:]:
        stop = bin.index('end', start)
        if stop > start:
            print sum(map(float, bin[start:stop])) / (stop-start)
        start = stop+1

如果您想跟踪平均值,可以存储它们并在函数结束时返回它们:

def average(bin):
    results = []
    start = 0
    while 'end' in bin[start:]:
        stop = bin.index('end', start)
        if stop > start:
            results.append(sum(map(float, bin[start:stop])) / (stop-start))
        start = stop+1
    return results

如果输入中的行中有两个“结束”标记,则“if stop> start:”行将阻止ZeroDivisionError。

对于更多Python魔术,你可以用yield替换print,你的函数将成为一个生成器:

def average(bin):
    start = 0
    while 'end' in bin[start:]:
        stop = bin.index('end', start)
        if stop > start:
            yield sum(map(float, bin[start:stop])) / (stop-start)
        start = stop+1

for a in average(bin):
    print a

答案 2 :(得分:1)

您的代码不起作用,因为您在每次循环中覆盖avg。您需要将每个集合的avg值存储为列表或其他内容。每当遇到count时,您还需要重置totalend。试试这个:

def average(bin):
    total = 0.0
    count=0
    avgs = []
    for number in bin:       
        if number == 'end':
            avgs.append(avg)
            total = 0.0
            count = 0
        else:
            total += float(number)
            count+=1
            avg = total/count

    return avgs

请注意,有更好的方法可以实现您的目标,但为了理解,我尝试尽可能少地对您的代码进行更改。

答案 3 :(得分:0)

这是一个解决方案(受其他人启发),它也给出了空集(NaN)的平均值,而不是忽略它们。

def average(bin):
  start=0    
  o=[]
  while 'end' in bin[start:]:
    stop = bin.index('end',start)
    s=1.0/(stop-start) if stop>start else float('nan')
    o.append(s*(sum(map(float, bin[start:stop]))))
    start=stop+1
  return o

和相应的一个衬垫(基于Cornett的衬垫)是:

[sum(k) / len(k) if k else float('nan') for k in [
 [float(j) for j in i.split() if j] for i in ' '.join(bin).split('end') if i]]