i searched for a solution to remove deplicates from two 2d list in python i couldn't find so here my question:
i have two lists, for example
[[1,2],[3,5],[4,4],[5,7]]
[[1,3],[4,4],[3,5],[3,5],[5,6]]
Expected result:
[[1,2],[1,3],[5,7],[5,6]]
I want to remove list inside on the lists that match EXACTLY the values of the other list.
my script:
def filter2dim(firstarray, secondarray):
unique = []
for i in range(len(firstarray)):
temp=firstarray[i]
for j in range(len(secondarray)):
if(temp == secondarray[j]):
break
elif(j==(len(secondarray)-1)):
unique.append(temp)
for i in range(len(secondarray)):
temp=secondarray[i]
for j in range(len(firstarray)):
if(temp == firstarray[j]):
break
elif(j==(len(firstarray)-1)):
unique.append(secondarray[i])
return
Please if you fix it and explain what you did it will be greateful. Thank you, Best Regards
Replace your 2-item lists with tuples and you can use set operations (because tuples are immutable and lists not, and set items must be immutable):
a = {(1,2),(3,5),(4,4),(5,7)}
b = {(1,3),(4,4),(3,5),(3,5),(5,6)}
print(a.symmetric_difference(b)) # {(1, 2), (1, 3), (5, 6), (5, 7)}
Note this also removes duplicates within each list because they are sets, and order is ignored.
If you need to programatically convert your lists into tuples, a list comprehension works just fine:
list_a = [[1,2],[3,5],[4,4],[5,7]]
set_a = {(i, j) for i, j in a}
print(set_a) # {(1, 2), (4, 4), (5, 7), (3, 5)}
Your script works fine for me, just add: return unique
Turn the first list into a dict
:
a = [[1, 2], [3, 5], [4, 4], [5, 7]]
b = [[1, 3], [4, 4], [3, 5], [3, 5], [5, 6]]
filt = dict(a)
result = [el for el in b if el[0] in filt and el[0] == filt[el[0]]]
Alternatively, turn the first list into a set of tuples, and just check for membership:
filt = set(map(tuple, a))
result = [el for el in b if tuple(el) in filt]
Both of these solutions avoid iterating through the first list more than once, because dict
and set
lookups are O(1).
来源:https://stackoverflow.com/questions/31919898/removing-duplicates-from-two-2-dimensional-list