<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Le 20/01/12 20:30, Vincent Vande Vyvre a écrit :
    <blockquote cite="mid:4F19C0CC.3080708@swing.be" type="cite">
      <meta content="text/html; charset=ISO-8859-1"
        http-equiv="Content-Type">
      <title></title>
      Le 20/01/12 19:49, Tamanna Sultana a écrit :
      <blockquote
cite="mid:feda1516-e842-4456-bbfb-2b9cdbe84e0f@u2g2000vbe.googlegroups.com"
        type="cite">
        <pre wrap="">can some one help me??
</pre>
        <blockquote type="cite">
          <pre wrap="">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
</pre>
        </blockquote>
        <pre wrap="">
</pre>
        <blockquote type="cite">
          <pre wrap="">['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']
</pre>
        </blockquote>
        <blockquote type="cite">
          <pre wrap="">If you can give me some lead to fix the code I wrote below that will be great:

</pre>
        </blockquote>
        <pre wrap="">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
</pre>
        <blockquote type="cite">
          <pre wrap="">Thnx
</pre>
        </blockquote>
      </blockquote>
      That's works:<br>
      <br>
      bin =['2598.95165', '2541.220308', '221068.0401', 'end',
      '4834.581952',<br>
      '1056.394859', '3010.609563', '2421.437603', '4619.861889',<br>
      ....<br>
      '2668.934414', '418.666014', '5216.392132', '760.894589',
      '8072.957639',<br>
      '346.5905371', 'end']<br>
      <br>
      avg = []<br>
      sp = ",".join(bin).split(',end')<br>
      for part in sp:<br>
          vals = part.split(',')<br>
          sum_ = 0<br>
          for v in vals:<br>
              if v:<br>
                  sum_ += float(v)<br>
              else:<br>
                  continue<br>
          avg.append(sum_ / len(vals))<br>
      <br>
      print avg<br>
      <br>
    </blockquote>
    OOps, that's works but the result is false.<br>
    <br>
    There is a final comma in "part" which causes a supernumerary
    element (empty) in vals.<br>
    <br>
    Correction:<br>
    <br>
    averages = []<br>
    items = []<br>
    for item in bin:<br>
        def get_average(vals):<br>
            return sum(vals) / len(vals)<br>
        try:<br>
            items.append(float(item))<br>
        except ValueError:<br>
            averages.append(get_average(items))<br>
            items = []<br>
    <br>
    print averages<br>
    <br>
    This assume the last element of bin is always "end".<br>
    <br>
    <div class="moz-signature">-- <br>
      Vincent V.V.<br>
      <a href="https://launchpad.net/oqapy">Oqapy</a> . <a
        href="https://launchpad.net/qarte+7">Qarte+7</a> . <a
        href="https://launchpad.net/paqager">PaQager</a></div>
  </body>
</html>