tags:

views:

78

answers:

5

I am trying to build a histogram of counts... so I create buckets. I know I could just go through and append a bunch of zeros i.e something along these lines:

buckets = [];
for i in xrange(0,100):
    buckets.append(0);

is there a more elegant way to do it? I feel like there should be a way to just declare an array of a certain size.

I know numpy has numpy.zeros but I want the more general solution

+4  A: 
buckets = [0] * 100
dan04
+1  A: 

buckets = [0]*100

mjhm
+1  A: 

The simplest solution would be

"\x00" * size # for a buffer of binary zeros
[0] * size # for a list of integer zeros

In general you should use more pythonic code like list comprehension (in your example: [0 for unused in xrange(100)]) or using string.join for buffers.

AndiDog
A: 

use numpy

import numpy
zarray = numpy.zeros(100)

And then use the Histogram library function

fabrizioM
A: 

Depending on what you're actually going to do with the data after it's collected, collections.defaultdict(int) might be useful.

Russell Borogove