I have a problem with a little Python script I wrote. Shortinfo:
What I have: an array consisting of arrays consisting of integers:
finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
What I want from this program: iterate over all sub-arrays, sort them, and delete double values (e.g. 52 in sub-array 0, 154 in sub-array 1 and 43 in sub-array 2)
My script so far:
finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
print "\n"
print "list:",finalSubLines,"\n========================"
for i in range (0, len(finalSubLines)):
finalSubLines[i].sort()
print "iterate until index:",len(finalSubLines[i])-2
print "before del-process:",finalSubLines[i]
for j in range (0, len(finalSubLines[i])-2):
print "j=",j
if finalSubLines[i][j] == finalSubLines[i][j+1]:
del finalSubLines[i][j]
print "after del-process:",finalSubLines[i]
print "-------------"
print finalSubLines
This is what I get:
The Problem:
- I don't understand why the range of the first for-loop is until len(finalSubLines) and not len(finalSubLines)-1. I tryed the latter, but then the last sub-array wasn't reached.
- The main problem is that the second for-loop won't reach all elements (blue rectangles in image). That's why the value 154 won't be deleted (see red rectangle in picture). Why is that happening?
Maybe there is an easier way to get what I need, but as I'm quite new to scripting, I don't know better ^^