Python Forum
IndexEroor : index out of range
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexEroor : index out of range
#1
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! :)
Reply
#2
Posting the code would be helpful.
Reply
#3
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.
Reply
#4
Sorry for the trouble!
This is the code!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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)
         
Reply
#5
and where is the full Traceback? in error tags...
Reply
#6
(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.
Reply
#7
Put print function after line 8 to see what glob() returns

1
print(files)
Check the values of point, ind2
Print them too before the if statement
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Index out of range error standenman 0 1,897 May-22-2023, 10:35 PM
Last Post: standenman
  [split] Getting Index Error - list index out of range krishna 2 3,584 Jan-09-2021, 08:29 AM
Last Post: buran
  Cycle through Numpy range within another range(?) Zero01 0 2,628 Jul-31-2020, 02:37 PM
Last Post: Zero01
  Getting Index Error - list index out of range RahulSingh 2 7,299 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 3,188 Jan-28-2020, 09:30 AM
Last Post: axelle
  iterate over index and define each range as a day karlito 7 6,634 Nov-19-2019, 06:37 AM
Last Post: karlito
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 21,122 Dec-12-2016, 10:51 PM
Last Post: m_lotinga
  Unable to understand reason for error IndexError: tuple index out of range rajat2504 4 55,605 Dec-09-2016, 11:04 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020