Issue
I have a list of elements which I'd like to check for double elements (no element will be there 3 times, just once or twice) and then I'd like to remove the original and the copy of such an element. So in the end I need a list that contains all elements which have only been present once in the original list.
The elements of the list are lines so I need to check both, end and start point against each other to see, which lines are the same. I already wrote some lines of which none seemed to work. I have already written a small function that checks the lines and finds doublets but I can't seem to work out how to delete both the copy and the original element in my list. This is the function that finds double lines:
def cmp_lines(l1, l2):
return(l1[0]==l2[0] and l1[1]==l2[1]) or (l1[0]==l2[1] and l1[1]==l2[0])
an example would be something like this:
list = [Line([1,0],[2,1]), Line([1,3],[4,5]), Line([2,1],[1,0])]
newList = [Line([1,3],[4,5])]
Using Python 2.x is a requirement.
Solution
you should write a function which takes as input the list of lines and filter it accordingly. That function will use cmp_lines to compare if two lines are equals. Adapt the following method according to your objects and classes definitions.
def filter_lines(lst) : # lst is the list of lines to filter
for i in range(len(lst)) :
is_unique = True
for j in range(i+1,len(lst)) :
if cmp_lines(lst[i],lst[j]) :
lst.pop(j) #remove the lst[j] from the list
is_unique = False #a same line was found
break;
if not is_unique : #The line lst[i] is unique
lst.pop(i) #remove lst[i] from the list
return lst
Answered By - florex
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.