Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index error
#1
I don't know why these codes makes error message.
Please help me


def dynamic_sort(C):
    C.sort(key = lambda x: x[1])  
    n = len(C)                   
    cache = []               
   
    for y in range(0, n):         
        cache.append(C[y][2])    
    
    return cache

def dynamic_job(C):
    cache = dynamic_sort(C)    
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]:         #this line makes the error message(IndexError: list index out of range)
                if cache[k] + C[i][2] > cache[i]:
                    cache[i] = cache[k] + C[i][2] 

    max = 0                      
    for i in range (0, n+1):     
        if max < cache[i]:        
            max = cache[i]       
    return max                    

    



C = [[2,5,6], [4,6,5], [6,7,4], [1,3,5], [5,8,11], [7,9,2]] 

print(dynamic_job(C))
buran write Dec-05-2020, 10:01 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Please, post the entire traceback that you get. We need to see the whole thing.
Take a time to read What to include in a post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Dec-05-2020, 09:51 AM)surim Wrote: I don't know why these codes makes error message.
Please help me


def dynamic_sort(C):
    C.sort(key = lambda x: x[1])  
    n = len(C)                   
    cache = []               
   
    for y in range(0, n):         
        cache.append(C[y][2])    
    
    return cache

def dynamic_job(C):
    cache = dynamic_sort(C)    
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]:         #this line makes the error message(IndexError: list index out of range)
                if cache[k] + C[i][2] > cache[i]:
                    cache[i] = cache[k] + C[i][2] 

    max = 0                      
    for i in range (0, n+1):     
        if max < cache[i]:        
            max = cache[i]       
    return max                    

    



C = [[2,5,6], [4,6,5], [6,7,4], [1,3,5], [5,8,11], [7,9,2]] 

print(dynamic_job(C))



Error:
Traceback (most recent call last): File "F:/알개/dynamin_jobschedule.py", line 32, in <module> print(dynamic_job(C)) File "F:/알개/dynamin_jobschedule.py", line 16, in dynamic_job if C[k][1] <= C[i][0]: IndexError: list index out of range
buran write Dec-05-2020, 10:06 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
You have been advised to do so.
Reply
#4
thank you for your reply and I added whole the error messages.
Reply
#5
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]: 
n is the number of things in C. In Python the first index is 0 and the last index is len-1. If n is 3 there is a C[0], C[1] and C[2]. If you tried to get C[3] you will get a "list index out of range" error because there is no C[3].

When you do the loop "for i in range(1, n+1)" you try to look at C[n]. Remember that the possible index numbers range from 0 to len-1, so C[len(C)] is out of range.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to resolve Index Error in my code? codify110 6 362 May-22-2021, 11:04 AM
Last Post: supuflounder
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 387 Nov-13-2020, 07:43 AM
Last Post: buran
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 606 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  List index out of range error while accessing 2 lists in python K11 2 588 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 809 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  IndexError: index 0 is out of bounds for axis 0 with size 0 error tmhsa 5 1,439 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  index error in text script Bartry 2 846 Dec-09-2019, 11:50 AM
Last Post: Malt
  ValueError: Index contains duplicate entries, cannot reshape” error when I try to use Smiling29 11 4,574 Oct-26-2019, 09:52 PM
Last Post: Smiling29
  I getting Index error after numerous tries priyanshu 3 986 Apr-21-2019, 05:15 PM
Last Post: ichabod801
  Index error using pop in nested loop PerksPlus 3 1,254 Mar-28-2019, 03:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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