Strange python error
Posted
by Werner
on Stack Overflow
See other posts from Stack Overflow
or by Werner
Published on 2010-03-16T19:41:13Z
Indexed on
2010/03/16
19:51 UTC
Read the original article
Hit count: 150
python
Hi,
I am trying to write a python program that calculates a histogram, given a list of numbers like:
1
3
2
3
4
5
3.2
4
2
2
so the input parameters are the filename and the number of intervals.
The program code is:
#!/usr/bin/env python
import os, sys, re, string, array, math
import numpy
Lista = []
db = sys.argv[1]
db_file = open(db,"r")
ic=0
nintervals= int(sys.argv[2])
while 1:
line = db_file.readline()
if not line:
break
ll=string.split(line)
#print ll[6]
Lista.insert(ic,float(ll[0]))
ic=ic+1
lmin=min(Lista)
print "min= ",lmin
lmax=max(Lista)
print "max= ",lmax
width=666.666
width=(lmax-lmin)/nintervals
print "width= ",width
nelements=len(Lista)
print "nelements= ",nelements
print " "
Histogram = numpy.zeros(shape=(nintervals))
for item in Lista:
#print item
int_number = 1 + int((item-lmin)/width)
print " "
print "item,lmin= ",item,lmin
print "(item-lmin)/width= ",(item-lmin)," / ",width," ====== ",(float(item)-float(lmin))/float(width)
print "int((item-lmin)/width)= ",int((item-lmin)/width)
print item , " belongs to interval ", int_number, " which is from ", lmin+width*(int_number-1), " to ",lmin+width*int_number
Histogram[int_number] = Histogram[int_number] + 1
4
but somehow I am completely lost, I get strange errors, can anybody help¿
Thanks
© Stack Overflow or respective owner