Le 20/01/12 20:30, Vincent Vande Vyvre a écrit :
Le 20/01/12 19:49, Tamanna Sultana a écrit :
can some one help me??
I would like to create a function that, given a bin, which is a list
(example below), 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

        
['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']
If you can give me some lead to fix the code I wrote below that will be great:

def average(bin):
    num=[]
    total = 0.0
    count=0
    for number in bin:
        while True:
            if number!='end':
                number=float(number)
                total += float(number)
                count+=1
                avg = total/count
            if number=='end':
                    break
    num.append(avg)
    return num
Thnx
That's works:

bin =['2598.95165', '2541.220308', '221068.0401', 'end', '4834.581952',
'1056.394859', '3010.609563', '2421.437603', '4619.861889',
....
'2668.934414', '418.666014', '5216.392132', '760.894589', '8072.957639',
'346.5905371', 'end']

avg = []
sp = ",".join(bin).split(',end')
for part in sp:
    vals = part.split(',')
    sum_ = 0
    for v in vals:
        if v:
            sum_ += float(v)
        else:
            continue
    avg.append(sum_ / len(vals))

print avg

OOps, that's works but the result is false.

There is a final comma in "part" which causes a supernumerary element (empty) in vals.

Correction:

averages = []
items = []
for item in bin:
    def get_average(vals):
        return sum(vals) / len(vals)
    try:
        items.append(float(item))
    except ValueError:
        averages.append(get_average(items))
        items = []

print averages

This assume the last element of bin is always "end".

--
Vincent V.V.
Oqapy . Qarte+7 . PaQager
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to