I have a piece of code from a larger script, and for the life of me I can't figure out what's causing the error.
It looks like this:
counter = 1
for i in range(len(binCounts)):
thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
OUTFILE.write("\t".join(bins[i][0:3]))
OUTFILE.write("\t")
OUTFILE.write(str(binCounts[i]))
OUTFILE.write("\t")
OUTFILE.write(str(thisRatio))
OUTFILE.write("\n")
where binCounts is a chronological list [1, 2, 3]
and bins is another list that contains slightly more info:
[['chrY', '28626328', '3064930174', '28718777', '92449', '49911'], ['chrY', '28718777', '3065022623', '28797881', '79104', '49911'], ['chrY', '28797881', '3065101727', '59373566', '30575685', '49912']]
It should be, for each variable in binCounts, taking the calculated thisRatio, the first 3 rows in bins, and the output of binCounts, and putting them together in a new file (OUTFILE).
But it's not doing this. It's giving me an error:
thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
ZeroDivisionError: float division by zero
When I run the line:
thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
interactively, it works fine.
When I break it into pieces, this is what I get:
A = float(binCounts[i])
print (A)
49999.0
B = (float(counter))
print (B)
1.0
C = float(len(bins))
print (C)
50000.0
thisRatio
2499950000.0
And then I reran the whole piece interactively (which I hadn't done before - just the single thisRatio line) and got this error...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
IndexError: list index out of range
So it seems when run as a .py script the error is a ZeroDivisionError, and when run interactively the error is an IndexError.
Related
I have a list of ~1,000 data files and at the beginning of my program I am splitting the file name into its certain parts in a for loop. The code runs for about 726 of the files and then I get the error
Traceback (most recent call last):
File "c:\Py Practice\ABBRH.py", line 58, in <module>
tube2 = sub3[2])
IndexError: list index out of range
I understand what the error is, but when looking at the file it stops on, there is an index. In fact, when I run the singular file by itself through the same code it works. The files are somewhat different, but they all should work the same.
the stopped file is "opimized.new.12_12_40-90-12_12_40.Ni00Nj00.lammps"
the numbers are arranged opimized.new.n1_m1_l1-angle-n2_m2_l2
sub1=(file.split('.'))
sub2=sub1[2]
sub3=(sub2.split('-'))
tube1 = sub3[0]
nma1 = (tube1.split('_'))
n1 = int(nma1[0])
m1 = int(nma1[1])
tube2 = sub3[2]
nma2 = (tube2.split('_'))
n2 = int(nma2[0])
m2 = int(nma2[1])
I want to access large range value as this higher end value will be change sometimes as I couldnt able to print last value, If I give lower range it is getting printed but when I give higher range like 73138176 or more than 7 digits it is getting memory error,as I am using Python 2.7.10, can anyone help me to get print the value of last range in this version of Python
lbas_on_bank = []
start=0
end=73138176
for lba in range(start,end):
if len(lbas_on_bank)>50:
lbas_on_bank = []
else:
lbas_on_bank.append(lba)
last_written_lba = lbas_on_bank[-1]
print(last_written_lba)
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
Use xrange() instead of range()
Or
try:
your code..
except MemoryError as er:
print(er)
Trying to adapt a basic profiler for bash scripts, block of code below.
Can't figure out why profiling code with a "if branch" throws exception (for example when running this code with a duplicate file). From what I've traced, it somewhere creates an extra index value. My apologies if this is trivial as I'm new to Python, but any suggestions on where could be the issue or how to remedy it would be greatly appreciated
def collect_timings(profiled_line, i):
if i == len(results) - 1:
return [0] + profiled_line
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
return [timing] + profiled_line
Error:
Traceback (most recent call last):
File "./profile", line 67, in <module>
main(sys.argv)
File "./profile", line 51, in main
profiling_time = map(collect_timings, results, range(len(results)))
File "./profile", line 24, in collect_timings
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
IndexError: list index out of range
Found the answer, posting in case it proves useful for someone.
The issues was a with a output redirection:
echo "Found '$file' is a duplicate of '${filecksums[$cksum]}'" >&2
That got passed as a separate entry to results:
["Found 'txt2/0122file.bck' is a duplicate of 'txt2/0113file.txt'\n"]
No idea how to make the code run with redirects, but it's not necessary, so I'll just avoid it.
you say:
if i == len(results) - 1:
below you say :
timing = float(results[i+1][1].replace(".N", ""))
so you check if "i" show the last element of results array (i.e. results has 5 cells from 0 to 4 and you check if i == 4( len(results) == 5 in this case AND NOT 4) ) and then you say results[i+1] which in the example above is equal to results[5] which is out of bounds.
Maybe you meant results[i]?
I have written a code in python to generate a sequence of ARIMA model's and determine their AIC values to compare them.The code is as below,
p=0
q=0
d=0
for p in range(5):
for d in range(1):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
print(arima_mod.params)
print arima_mod.aic()
I am getting a error message as below,
TypeError Traceback (most recent call last)
<ipython-input-60-b662b0c42796> in <module>()
8 arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
9 print(arima_mod.params)
---> 10 print arima_mod.aic()
global arima_mod.aic = 1262.2449736558815
11
**TypeError: 'numpy.float64' object is not callable**
Remove the brackets after print arima_mod.aic(). As I read it, arima_mod.aic is 1262.2449736558815, and thus a float. The brackets make python think it is a function, and tries to call it. You do not want that (because it breaks), you just want that value. So remove the brackets, and you'll be fine.
import sys
def func():
T = int(next(sys.stdin))
for i in range(0,T):
N = int(next(sys.stdin))
print (N)
func()
Here I am taking input T for for loop and iterating over T it gives Runtime error time: 0.1 memory: 10088 signal:-1 again-again . I have tried using sys.stdin.readline() it also giving same error .
I looked at your code at http://ideone.com/8U5zTQ . at the code itself looks fine, but your input can't be processed.
Because it is:
5 24 2
which will be the string:
"5 24 2"
this is not nearly an int, even if you try to cast it. So you could transform it to the a list with:
inputlist = next(sys.stdin[:-2]).split(" ")
to get the integers in a list that you are putting in one line. The loop over that.
After that the code would still be in loop because it want 2 integers more but at least you get some output.
Since I am not totally shure what you try to achieve, you could now iterate over that list and print your inputs:
inputlist = next(sys.stdin[:-2]).split(" ")
for i in inputlist
print(i)
Another solution would be, you just put one number per line in, that would work also
so instead of
5 24 2
you put in
5
24
2
Further Advice
on Ideone you also have an Error Traceback at the bottom auf the page:
Traceback (most recent call last):
File "./prog.py", line 8, in <module>
File "./prog.py", line 3, in func
ValueError: invalid literal for int() with base 10: '1 5 24 2\n'
which showed you that it can't handle your input