sum function

Mike mike20007 at gmail.com
Fri Oct 5 09:47:12 EDT 2012


On Friday, October 5, 2012 9:41:44 AM UTC-4, Ramchandra Apte wrote:
> On Friday, 5 October 2012 19:09:15 UTC+5:30, Mike  wrote:
> 
> > On Thursday, October 4, 2012 4:52:50 PM UTC-4, Mike wrote:
> 
> > 
> 
> > > 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?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Any thoughts please?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Thanks
> 
> > 
> 
> > 
> 
> > 
> 
> > Sorry about that. Here you go
> 
> > 
> 
> > 
> 
> > 
> 
> > Traceback (most recent call last):
> 
> > 
> 
> >   File "test.py", line 17, in <module>
> 
> > 
> 
> >     total = sum(float(col.value) for r in iter(next_r, None) for col in r[0].columns.itervalues())
> 
> > 
> 
> >   File "test.py", line 17, in <genexpr>
> 
> > 
> 
> >     total = sum(float(col.value) for r in iter(next_r, None) for col in r[0].columns.itervalues())
> 
> > 
> 
> > IndexError: list index out of range
> 
> 
> 
> the variable "r" is an empty list

Here is the actual code.

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


Scanner does have rows.

Are we missing something please?

Thanks



More information about the Python-list mailing list