Delete duplicate tuples with same elements in nested list Python

非 Y 不嫁゛ 提交于 2019-12-07 14:56:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!