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


Groups > comp.lang.python > #19159 > unrolled thread

can some one help me with my code. thanks

Started byTamanna Sultana <tamannas.rahman@gmail.com>
First post2012-01-20 10:49 -0800
Last post2012-01-23 05:47 -0800
Articles 10 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#19159 — can some one help me with my code. thanks

FromTamanna Sultana <tamannas.rahman@gmail.com>
Date2012-01-20 10:49 -0800
Subjectcan some one help me with my code. thanks
Message-ID<feda1516-e842-4456-bbfb-2b9cdbe84e0f@u2g2000vbe.googlegroups.com>
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

[toc] | [next] | [standalone]


#19163

FromTerry Reedy <tjreedy@udel.edu>
Date2012-01-20 14:46 -0500
Message-ID<mailman.4892.1327088810.27778.python-list@python.org>
In reply to#19159
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]
-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#19169

FromTim Chase <python.list@tim.thechases.com>
Date2012-01-20 14:17 -0600
Message-ID<mailman.4897.1327094021.27778.python-list@python.org>
In reply to#19159
On 01/20/12 13:46, Terry Reedy wrote:
> 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(['end']))

ka-blooie! :-)

would be worth noting what should happen in the event of an empty 
bin.

-tkc


[toc] | [prev] | [next] | [standalone]


#19172

FromTerry Reedy <tjreedy@udel.edu>
Date2012-01-20 16:26 -0500
Message-ID<mailman.4900.1327094828.27778.python-list@python.org>
In reply to#19159
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

[toc] | [prev] | [next] | [standalone]


#19179

FromJon Clements <joncle@googlemail.com>
Date2012-01-20 17:30 -0800
Message-ID<e83dd463-aa1c-4672-b892-648cc2418853@c20g2000vbb.googlegroups.com>
In reply to#19172
On Jan 20, 9:26 pm, Terry Reedy <tjre...@udel.edu> wrote:
> 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

Not directing this at you Terry, and you and Tim have made fine points
-- this just appears to me to be the best point at which to respond to
a thread.

To the OP - you have great answers, and, please note this just happens
to be the way I would do this.

I would separate the parsing of the data, and the calculation code
out. I've whipped this up rather quickly, so it might have a few flaws
but...

from itertools import groupby
def partition(iterable, sep=lambda L: L == 'end', factory=float):
    for key, vals in groupby(iterable, sep):
        if not key: yield map(factory, vals)

# And a pure cheat, but useful if more complex calculations are
required etc... (Plus covers NaN)
import numpy as np
print map(np.mean, partition(bin))

What you've got will work though, so wouldn't worry too much and this
is just my 2p,

Jon.

[toc] | [prev] | [next] | [standalone]


#19174

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2012-01-20 16:21 -0800
Message-ID<38c1fdd1-6bd1-4e4c-b0c7-ea47f75c9e86@b23g2000yqn.googlegroups.com>
In reply to#19159
On Jan 20, 12:49 pm, Tamanna Sultana <tamannas.rah...@gmail.com>
wrote:
> > If you can give me some lead to fix the code I wrote below that will be great:

Your variable names need a bit more thought

> def average(bin):

What is a "bin"? Maybe you shoulc have called this a "lst" eh?

>     num=[]
Why would you call a list of numbers (that's plural BTW) the singular
name "num"? Not to mention that even "numbers" is the wrong
identifier. Use "averages" instead. Heck, even "buffer" would have
been a better choice that "num".

>     total = 0.0
>     count = 0
>     for number in bin:

Not every "item" in the lst is a number so we should use the generic
"item" identifier here.

>         while True:
What the hell is a loop doing here???

>             if number!='end':
>                 number=float(number)
>                 total += float(number)
>                 count+=1
>                 avg = total/count
>             if number=='end':
>                     break

This block of logic is ugly. Try this instead...

>             if number == 'end':
>                 break
>             number=float(number)
>             total += float(number)
>             count+=1
>             avg = total/count

...but that whole loop thing is nonsense anyway. I would reconsider
this code completely. Here is an outline:

for item in lst
    if item equals "end":
        compute the current average from buffers
    else:
        increment the running total
        increment the count.

[toc] | [prev] | [next] | [standalone]


#19180

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-01-21 02:23 +0000
Message-ID<4f1a2197$0$29987$c3e8da3$5496439d@news.astraweb.com>
In reply to#19174
On Fri, 20 Jan 2012 16:21:30 -0800, Rick Johnson wrote:

> On Jan 20, 12:49 pm, Tamanna Sultana <tamannas.rah...@gmail.com> wrote:
>> > If you can give me some lead to fix the code I wrote below that will
>> > be great:
> 
> Your variable names need a bit more thought
> 
>> def average(bin):
> 
> What is a "bin"? Maybe you shoulc have called this a "lst" eh?

What's a 'lst'? It's not even a real word, and it looks like 1st.

"Bin" is a standard English world. You know, like "rubbish bin" or 
"recycling bin". It is also a standard term used in statistics as a noun, 
a verb, and adjective, e.g.:

http://stackoverflow.com/questions/5581023/r-graphing-binned-data



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#19183

FromChris Angelico <rosuav@gmail.com>
Date2012-01-21 15:03 +1100
Message-ID<mailman.4903.1327118602.27778.python-list@python.org>
In reply to#19180
On Sat, Jan 21, 2012 at 1:23 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Fri, 20 Jan 2012 16:21:30 -0800, Rick Johnson wrote:
>> Your variable names need a bit more thought
>>
>>> def average(bin):
>>
>> What is a "bin"? Maybe you shoulc have called this a "lst" eh?
>
> "Bin" is a standard English world. You know, like "rubbish bin" or
> "recycling bin".

Or my first thought: stock location. Inventory software often doesn't
care whether your physical stock is organized by shelf, box,
warehouse, planet, or secret-space-on-Firefly-class-ship; just number
each location, and that's the bin number. (And no, that isn't like
"PIN number".) It's then quite logical to want various stats to be
per-bin, which would lead exactly to the OP's problem - including the
odd notation of input data, all too likely in a real-world scenario.

ChrisA

[toc] | [prev] | [next] | [standalone]


#19263

FromTamanna Sultana <tamannas.rahman@gmail.com>
Date2012-01-23 05:47 -0800
Message-ID<a1229832-2516-4063-a491-546489d0a2db@8g2000vbv.googlegroups.com>
In reply to#19183
On Jan 20, 11:03 pm, Chris Angelico <ros...@gmail.com> wrote:
> On Sat, Jan 21, 2012 at 1:23 PM, Steven D'Aprano
>
> <steve+comp.lang.pyt...@pearwood.info> wrote:
> > On Fri, 20 Jan 2012 16:21:30 -0800, Rick Johnson wrote:
> >> Your variable names need a bit more thought
>
> >>> def average(bin):
>
> >> What is a "bin"? Maybe you shoulc have called this a "lst" eh?
>
> > "Bin" is a standard English world. You know, like "rubbish bin" or
> > "recycling bin".
>
> Or my first thought: stock location. Inventory software often doesn't
> care whether your physical stock is organized by shelf, box,
> warehouse, planet, or secret-space-on-Firefly-class-ship; just number
> each location, and that's the bin number. (And no, that isn't like
> "PIN number".) It's then quite logical to want various stats to be
> per-bin, which would lead exactly to the OP's problem - including the
> odd notation of input data, all too likely in a real-world scenario.
>
> ChrisA

Thanx all.

[toc] | [prev] | [next] | [standalone]


#19264

FromTamanna Sultana <tamannas.rahman@gmail.com>
Date2012-01-23 05:47 -0800
Message-ID<98da3993-ea73-4574-b4e0-3ad9ff048bb3@z31g2000vbt.googlegroups.com>
In reply to#19180
On Jan 20, 9:23 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Fri, 20 Jan 2012 16:21:30 -0800, Rick Johnson wrote:
> > On Jan 20, 12:49 pm, Tamanna Sultana <tamannas.rah...@gmail.com> wrote:
> >> > If you can give me some lead to fix the code I wrote below that will
> >> > be great:
>
> > Your variable names need a bit more thought
>
> >> def average(bin):
>
> > What is a "bin"? Maybe you shoulc have called this a "lst" eh?
>
> What's a 'lst'? It's not even a real word, and it looks like 1st.
>
> "Bin" is a standard English world. You know, like "rubbish bin" or
> "recycling bin". It is also a standard term used in statistics as a noun,
> a verb, and adjective, e.g.:
>
> http://stackoverflow.com/questions/5581023/r-graphing-binned-data
>
> --
> Steven

thanx

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web