Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'lines.': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'me??': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; "'>'": 0.16; 'overly': 0.16; 'reedy': 0.16; 'reposted': 0.16; "tim's": 0.16; 'wrote:': 0.16; '>>>': 0.18; 'jan': 0.19; 'subject:help': 0.21; 'input': 0.21; 'header:In-Reply-To:1': 0.22; 'asked': 0.23; 'happen.': 0.23; 'replacing': 0.23; 'welcome.': 0.23; 'string': 0.24; 'format,': 0.24; 'code': 0.25; 'pm,': 0.26; 'function': 0.27; 'skip:[ 10': 0.27; 'compare': 0.28; '(this': 0.28; 'lists': 0.28; "skip:' 10": 0.29; 'lines': 0.30; 'collections': 0.30; 'separated': 0.30; 'subject:some': 0.30; 'that,': 0.32; 'list': 0.32; 'header:User-Agent:1': 0.33; 'to:addr :python-list': 0.33; 'decide': 0.33; 'there': 0.33; 'turned': 0.33; 'header:X-Complaints-To:1': 0.34; 'sets': 0.35; 'beginning': 0.36; 'but': 0.37; 'received:org': 0.37; 'happens': 0.37; 'subject:can': 0.37; 'could': 0.37; 'some': 0.38; 'subject:with': 0.38; 'easier': 0.38; 'sometimes': 0.38; 'bin': 0.38; 'help': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'data': 0.40; 'more': 0.61; 'total': 0.61; 'your': 0.61; 'subject:. ': 0.64; 'subject:one': 0.77; 'comment.': 0.84; 'num': 0.84; 'subject:thanks': 0.84; '0.0': 0.91; "u're": 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: can some one help me with my code. thanks Date: Fri, 20 Jan 2012 16:26:45 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327094828 news.xs4all.nl 6974 [2001:888:2000:d::a6]:50895 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19172 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