Topic: how to calculate average for a range of numbers separated by a string

['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', 'end']

I would like to create a function that, given a bin, which is a list (example above), generates averages for the numbers separated by a string 'end'. I am expecting to have 4 averages from the above bin, since there are 4 sets of numbers separated by 4 'end' strings

can someone help me fix my code below which does not seem to work:

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

Thumbs up

Re: how to calculate average for a range of numbers separated by a string

>>> lst = ['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', 'end']
>>> 
>>> def func(lst):
...     avrlst = []
...     total = n = 0
...     for i in lst:
...         if i != 'end':
...             total += float(i)
...             n += 1
...         else:
...             avrlst.append(total / n)
...             total = n = 0
...     return avrlst
... 
>>> print func(lst)
[75402.73735266666, 6038.873234763999, 4566.874299, 19005.346434449995]
>>>

Thumbs up