Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   sum function (http://www.velocityreviews.com/forums/t953048-sum-function.html)

 mike20007@gmail.com 10-04-2012 08:52 PM

sum function

Hi All,

I am new to python and am getting the data from hbase.
I am trying to do sum on the column as below

scanner = client.scannerOpenWithStop("tab", "10", "1000", ["cf:col1"])
total = 0.0
r = client.scannerGet(scanner)
while r:
for k in (r[0].columns):
total += float(r[0].columns[k].value)
r = client.scannerGet(scanner)

print total

Do you know of better (faster) way to do sum?

Thanks

 Ian Kelly 10-04-2012 09:04 PM

Re: sum function

On Thu, Oct 4, 2012 at 2:52 PM, <mike20007@gmail.com> wrote:
> scanner = client.scannerOpenWithStop("tab", "10", "1000", ["cf:col1"])
> total = 0.0
> r = client.scannerGet(scanner)
> while r:
> for k in (r[0].columns):
> total += float(r[0].columns[k].value)
> r = client.scannerGet(scanner)
>
> print total
>
> Do you know of better (faster) way to do sum?

scanner = client.scannerOpenWithStop("tab", "10", "1000", ["cf:col1"])
next_r = itertools.partial(client.scannerGet, scanner)
total = sum(float(col.value) for r in iter(next_r, None) for col in
r.itervalues())

 Ian Kelly 10-04-2012 09:05 PM

Re: sum function

On Thu, Oct 4, 2012 at 3:04 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> scanner = client.scannerOpenWithStop("tab", "10", "1000", ["cf:col1"])
> next_r = itertools.partial(client.scannerGet, scanner)
> total = sum(float(col.value) for r in iter(next_r, None) for col in
> r.itervalues())

That should be "functools" above, not "itertools". :-P

 Mike 10-04-2012 09:29 PM

Re: sum function

I get below error

NameError: name 'functools' is not defined

Thanks

 Mike 10-04-2012 09:29 PM

Re: sum function

I get below error

NameError: name 'functools' is not defined

Thanks

 Mike 10-04-2012 09:31 PM

Re: sum function

Thanks Ian for the quick reply.

I get the below error.

NameError: name 'itertools' is not defined

Thanks

 Mike 10-04-2012 09:31 PM

Re: sum function

Thanks Ian for the quick reply.

I get the below error.

NameError: name 'itertools' is not defined

Thanks

 Chris Angelico 10-04-2012 09:34 PM

Re: sum function

On Fri, Oct 5, 2012 at 7:29 AM, Mike <mike20007@gmail.com> wrote:
> I get below error
>
> NameError: name 'functools' is not defined
>
> Thanks

functools is a module:

import functools

ChrisA

 Dave Angel 10-04-2012 09:39 PM

Re: sum function

On 10/04/2012 05:29 PM, Mike wrote:
> I get below error
>
> NameError: name 'functools' is not defined
>

functools is a module in the standard library. You need to import it.

import functools

--

DaveA

 Mike 10-05-2012 12:40 AM

Re: sum function

On Thursday, October 4, 2012 5:40:26 PM UTC-4, Dave Angel wrote:
> On 10/04/2012 05:29 PM, Mike wrote:
>
> > I get below error

>
> >

>
> > NameError: name 'functools' is not defined

>
> >

>
>
>
> functools is a module in the standard library. You need to import it.
>
>
>
> import functools
>
>
>
>
>
>
>
> --
>
>
>
> DaveA

I imported functools. Now I get the below error please.

Traceback (most recent call last):
File "test.py", line 16, in <module>
total = sum(float(col.value) for r in iter(next_r, None) for col in r.itervalues())
File "test.py", line 16, in <genexpr>
total = sum(float(col.value) for r in iter(next_r, None) for col in r.itervalues())
AttributeError: 'list' object has no attribute 'itervalues'

Thanks

All times are GMT. The time now is 06:52 PM.