Python Forum
IndexEroor : index out of range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: IndexEroor : index out of range (/thread-3345.html)



IndexEroor : index out of range - adithyakrish - May-16-2017

This is the error I get when I try running a certain program. The fun part is the program runs successfully in Windows but displays this error in Ubuntu.
I even tried updating the python from 2.7 to 3.5.
Please help me! :)


RE: IndexEroor : index out of range - gohanzdad - May-16-2017

Posting the code would be helpful.


RE: IndexEroor : index out of range - buran - May-16-2017

you can not really expect we can help without seeing your code. Please post your code (small snippet that reproduce the error) in python tags, as well as full Traceback in error tags.


RE: IndexEroor : index out of range - adithyakrish - May-17-2017

Sorry for the trouble!
This is the code!

import numpy as np
import matplotlib.pyplot as plt
import operator
import glob
import pylab
from matplotlib.ticker import MultipleLocator

files = sorted(glob.glob('S:/netlist/netlist*'))
file_dict = {}
cnt = 1
for filename in files:
    f = open(filename)
    dict = {}
    lines = f.readlines()
    for l in lines:
        ind = l[0]+l[2]+l[4]
        if ind not in dict:
            dict[ind] = cnt
            cnt = cnt + 1
    file_dict[filename] = dict

for i in range(0,len(files)):
    sorted_dict = sorted(file_dict[files[i]].items(), key=operator.itemgetter(1))
    for t in sorted_dict:
        print(t)

def hex_string_to_bin_string(input):
   lookup = {"0" : "0000", "1" : "0001", "2" : "0010", "3" : "0011", "4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", "8" : "1000", "9" : "1001", "A" : "1010", "B" : "1011", "C" : "1100", "D" : "1101", "E" : "1110", "F" : "1111"}
   result = ""
   for byte in input:
      result =  result + lookup[byte]
   return result

def hex_string_to_hex_value(input):
   value = hex_string_to_bin_string(input)
   highest_order = len(value) - 1
   result = 0
   for bit in value:
      result = result + int(bit) * pow(2,highest_order)
      highest_order = highest_order - 1
   return hex(result)

def on_press(event):
    print ('you pressed', event.button, event.xdata, event.ydata)
    event.canvas.figure.clear()
    # select new curves to plot, in this example [1,2,3] [0,0,0]
    event.canvas.figure.gca().plot([1,2,3],[0,0,0], 'ro-')
    event.canvas.figure.gca().grid()
    event.canvas.figure.gca().legend()
    event.canvas.draw()

a = open('full_list.txt')
lines = a.readlines()
for b in lines:
    if ((b[0] == '0') and (b[1] != ' ')):
        X = b[1] + b[2]
    else:
        index = b[0] + b[2] + b[4] + b[6]
        hexa = hex_string_to_hex_value(index)
        #print (hexa)
        dec = int(hexa, base = 16)
        #print (dec)
        my_array = []
        while(dec!=0):
            if ((dec % 2) == 0):
                y = 0
                dec = int(dec/2)
                my_array.append(y)
            else:
                y = 1
                dec = int(dec/2)
                my_array.append(y)        
        my_array.reverse()
        #print (my_array)
        #print ('Length of the array is', len(my_array))
        i = len(my_array)
        while (i < 16):
            my_array.insert(0, 0)
            i = i + 1
        #print (my_array)
        if (my_array[0] == 0):
            if (my_array[1] == 1):
                iden1 = (256 * b[2]) + (16 * b[4]) + b[6]
                iden = int(iden1, base = 16)
                #print ('Chip ID','-',iden)
        else:
            point = iden - 2
            ind2 = b[2] + b[4] + b[6]
            if (file_dict[files[point]].get(ind2)):
                Y = file_dict[files[point]][ind2]
                #print(X,' ',Y)
                plt.plot([X], [Y], marker = 'o', markersize = 2, color = "black")
                ax = plt.gca()
                ax.xaxis.grid(True)
            else:
                print ('Unknown Spike',' ','-',' ',ind2)
            #print (X, ',', Y)
#pylab.xlim([0 , max(X)])
#pylab.ylim([0, max(Y)])
plt.show()
figure.canvas.mpl_connect('button_press_event', on_press)
        



RE: IndexEroor : index out of range - buran - May-17-2017

and where is the full Traceback? in error tags...


RE: IndexEroor : index out of range - adithyakrish - May-19-2017

(May-17-2017, 09:38 PM)buran Wrote: and where is the full Traceback? in error tags...
Traceback ( most recent call last):
      File "New_Program.py", line 89, in <module>
        if (file_dict[files[point]].get(ind2)):
IndexError : list index out of range

The "full_list.txt" is
01
4 0 0 2
8 0 0 1
8 0 0 2
8 0 0 3
8 0 1 1
8 0 1 2
8 0 1 3
8 0 2 1
8 0 2 2
8 0 2 3
8 0 3 1
8 0 3 2
8 0 3 3
02
4 0 0 2
8 0 1 0
03
4 0 0 2
8 0 2 0
04
4 0 0 2
8 0 3 0
05
4 0 0 2
8 0 0 0

the netlist is :

0 0 0 1 0 1 163840000
0 1 0 2 0 1 163840000
0 2 0 3 0 1 163840000
0 3 0 0 0 1 163840000


I used glob because there will be more files in there (in the future) only with a small difference.


RE: IndexEroor : index out of range - wavic - May-19-2017

Put print function after line 8 to see what glob() returns

print(files)
Check the values of point, ind2
Print them too before the if statement


RE: IndexEroor : index out of range - buran - May-19-2017

I would also print len(files) and point.

note that dict is built-in function, so better choose another name not to overwrite the built-in function