2014-11-02 96 views
3

我有一个元组列表,我需要删除包含相同元素的元组。删除嵌套列表中具有相同元素的重复元组Python

d = [(1,0),(2,3),(3,2),(0,1)]

OutputRequired = [(1,0),(2,3)]输出顺序无关紧要

命令集()不能按预期工作。

+1

请阅读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask),编辑您的问题并重新提出您的问题。至少你想使用的编程语言应该被标记。 – Hille 2014-11-02 18:22:35

+0

这个答案有一个整洁的解决方案,https://stackoverflow.com/questions/40850892/delete-duplicate-tuples-independent-of-order-with-same-elements-in-generator-pyt?rq=1 – 2017-08-16 04:03:16

回答

3

在这个解决方案中,我们在检查temp是否已存在,然后将其复制回d后,将每个元组复制到temp中。

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 

这会给出预期的输出。

相关问题