3

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:

  1. 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.
  2. 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 ^^

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Elias
  • 41
  • 4

3 Answers3

3

The easy way would be to use sets:

[sorted(set(sub)) for sub in finalSubLines]

Demo:

>>> finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
>>> [sorted(set(sub)) for sub in finalSubLines]
[[0, 44, 52, 57], [12, 25, 154], [41, 42, 43, 74]]

Your loop doesn't take into account that by removing items, your lists are getting shorter; what was once at index i + 1 moved to index i, but your loop is happily moving on to index i + 1, and the value there was once located at i + 2. Thus you skip elements.

See Loop "Forgets" to Remove Some Items for a more detailed description of what happens.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

If you actually want to remove elements that appear exactly twice:

finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]

from collections import Counter
counts = [Counter(sub) for sub in finalSubLines]

print([sorted(k for k,v in c.iteritems() if v != 2) for c in counts])
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Try this:

tempList=[]
finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
for item in finalSubLines:
    tempList.append(sorted(set(sorted(item))))
print tempList,
Adam S
  • 16,144
  • 6
  • 54
  • 81
Ash
  • 355
  • 1
  • 2
  • 7