我有一个元组列表,我需要删除包含相同元素的元组。
d=[(1,0),(2,3),(3,2),(0,1)]
OutputRequired=[(1,0),(2,3)] 输出顺序无关紧要
命令 set() 没有按预期工作。
最佳答案
在这个解决方案中,我将每个元组复制到 temp
中。在检查它是否已经存在于 temp
之后然后复制回 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
这将按预期提供输出。
关于python-2.7 - 删除嵌套列表Python中具有相同元素的重复元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702664/