list index out of range: python -
i have list has these elements(list consists str(elements)):
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
and want process have output this
['-0', '1#', '15#']
if element -x want leave there take last 2 elements , if gap 1 remove element before last element. here code:
k in range(len(l1)): if "-" in (l1[-k] or l1[-k-1]): print("debuggggg") pass elif(int(l1[-k]) - int(l1[-k-1])== 1 , int(l1[-k]) - int(l1[-k-2])== 2) : a= l1[-2] print(a) l1.remove(a) #print("debug 2") elif(int(l1[-k]) - int(l1[-k-1])== 1): a= l1[-2] l1.remove(a) l1[-2] = l1[-2] +"#" l1[-1] = l1[-1] +"#" print("3") #elif(type(l1[-2]) str): #pass
problem here :
debuggggg 14 13 12 11 10 9 8
the last 2 elements not include char "-" seems do. furthermore after 8 loop script crashes:
elif(int(l1[-k]) - int(l1[-k-1])== 1 , int(l1[-k]) - int(l1[-k-2])== 2) : indexerror: list index out of range
but not out range of list. problem?
your second question
but not out range of list. problem?
it out of range error.
l1[-len(l1)-1]
raises error. doing in code.
for k in range(len(l1)): ... elif(int(l1[-k]) - int(l1[-k-1])== 1 , int(l1[-k]) - int(l1[-k-2])== 2) :#<--here(l1[-k-2])
Comments
Post a Comment