Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19172

Re: can some one help me with my code. thanks

From Terry Reedy <tjreedy@udel.edu>
Subject Re: can some one help me with my code. thanks
Date 2012-01-20 16:26 -0500
References <feda1516-e842-4456-bbfb-2b9cdbe84e0f@u2g2000vbe.googlegroups.com> <jfcgap$8j4$1@dough.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.4900.1327094828.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 1/20/2012 2:46 PM, Terry Reedy wrote:
> On 1/20/2012 1:49 PM, Tamanna Sultana wrote:
>>
>> 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
>
> [Posting your overly long set of data lines with a '>' quote at the
> beginning of each line was a nuisance. Reposted with few lines. I will
> let you compare your code to mine.]
>
> bin = ['2598.95165', '2541.220308', '221068.0401', 'end', '4834.581952',
> '1056.394859', '3010.609563', '2421.437603', '4619.861889',
> '3682.012227', '3371.092883', '6651.509488', '7906.092773',
> '7297.133447', 'end', '4566.874299', 'end', '4255.700077',
> '1857.648393', '11289.48095', '2070.981805', '1817.505094',
> '563.0265409', '70796.45356', '565.2123689', '6560.030116',
> '2668.934414', '418.666014', '5216.392132', '760.894589', '8072.957639',
> '346.5905371', 'end']
>
> def average(bin):
> num=[]
> total = 0.0
> count=0
> for number in bin:
> if number!='end':
> total += float(number)
> count+=1
> else:
> num.append(total/count)
> total = 0.0
> count= 0
> return num
>
> print(average(bin))
>
>  >>>
> [75402.73735266666, 4485.0726684, 4566.874299, 7817.36494866]

U're welcome. But do notice Tim's comment. In non-toy situations, you 
have to decide how to handle empty collections (return float('nan')?), 
or whether to just let whatever happens happen.

If you control the input format, a list of lists would be easier than an 
end marker. But sometimes one is handed data and asked to process it as is.

Also note (this is a more advanced topic) that average() could be turned 
into a generator function by replacing 'num.append(total/count)' with 
'yield total/count' and removing the initialization and return of num.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

can some one help me with my code. thanks Tamanna Sultana <tamannas.rahman@gmail.com> - 2012-01-20 10:49 -0800
  Re: can some one help me with my code. thanks Terry Reedy <tjreedy@udel.edu> - 2012-01-20 14:46 -0500
  Re: can some one help me with my code. thanks Tim Chase <python.list@tim.thechases.com> - 2012-01-20 14:17 -0600
  Re: can some one help me with my code. thanks Terry Reedy <tjreedy@udel.edu> - 2012-01-20 16:26 -0500
    Re: can some one help me with my code. thanks Jon Clements <joncle@googlemail.com> - 2012-01-20 17:30 -0800
  Re: can some one help me with my code. thanks Rick Johnson <rantingrickjohnson@gmail.com> - 2012-01-20 16:21 -0800
    Re: can some one help me with my code. thanks Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-21 02:23 +0000
      Re: can some one help me with my code. thanks Chris Angelico <rosuav@gmail.com> - 2012-01-21 15:03 +1100
        Re: can some one help me with my code. thanks Tamanna Sultana <tamannas.rahman@gmail.com> - 2012-01-23 05:47 -0800
      Re: can some one help me with my code. thanks Tamanna Sultana <tamannas.rahman@gmail.com> - 2012-01-23 05:47 -0800

csiph-web