问题
I have a list of tuples and I need to delete tuples containing same elements.
d=[(1,0),(2,3),(3,2),(0,1)]
OutputRequired=[(1,0),(2,3)] Order of output doesn't matter
command set() doesn't work as expected.
回答1:
In this solution, I am copying each of the tuples into a temp
after checking whether it is already present in the temp
and then copy back to d
.
d = [(1,0),(2,3),(3,2),(0,1)]
temp = []
for a,b in d :
if (a,b) not in temp and (b,a) not in temp: #to check for the duplicate tuples
temp.append((a,b))
d = temp * 1 #copy temp to d
This will give the output as expected.
来源:https://stackoverflow.com/questions/26702664/delete-duplicate-tuples-with-same-elements-in-nested-list-python