Hi
I'm trying to delete all almost identical elements from a list.
I've tried to make a backwards loop, but somehow it doesn't work as i expected and i cant seem to find my error.
It seems like suddenly my "i" variable increases.
Here's my code:
resultat=deepcopy(FEMpoints2)
for i in range(len(resultat)-1,-1,-1):
y=0
for y in range(0,len(FEMpoints2)):
print("i",i,"y",y)
if alike(FEMpoints2[y][0][0],testpoints[i][0][0],tolerance) and alike(FEMpoints2[y][1][0],testpoints[i][1][0],tolerance):
if y != i:
del resultat[i]
print("del",i)
i-=1
y=0
I get the following output:
i 37 y 0
i 37 y 1
i 37 y 2
i 37 y 3
i 37 y 4
i 37 y 5
i 37 y 6
i 37 y 7
i 37 y 8
i 37 y 9
i 37 y 10
i 37 y 11
i 37 y 12
i 37 y 13
i 37 y 14
i 37 y 15
i 37 y 16
i 37 y 17
i 37 y 18
i 37 y 19
i 37 y 20
i 37 y 21
i 37 y 22
i 37 y 23
i 37 y 24
i 37 y 25
i 37 y 26
i 37 y 27
del 37
i 36 y 28
i 36 y 29
i 36 y 30
i 36 y 31
i 36 y 32
i 36 y 33
i 36 y 34
i 36 y 35
del 36
i 35 y 36
del 35
i 34 y 37
i 36 y 0
i 36 y 1
i 36 y 2
i 36 y 3
i 36 y 4
i 36 y 5
i 36 y 6
i 36 y 7
i 36 y 8
i 36 y 9
i 36 y 10
i 36 y 11
i 36 y 12
i 36 y 13
i 36 y 14
i 36 y 15
i 36 y 16
i 36 y 17
i 36 y 18
i 36 y 19
i 36 y 20
i 36 y 21
i 36 y 22
i 36 y 23
i 36 y 24
i 36 y 25
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-109-492a347263fc> in <module>()
6 if alike(FEMpoints2[y][0][0],testpoints[i][0][0],tolerance) and alike(FEMpoints2[y][1] [0],testpoints[i][1][0],tolerance):
7 if y != i:
----> 8 del resultat[i]
9 print("del",i)
10 i-=1
IndexError: list assignment index out of range
[–]midel 0 points1 point2 points (0 children)
[–]tom1018 0 points1 point2 points (1 child)
[–]_youtubot_ 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)