本文介绍了在嵌套列表Python中删除具有相同元素的重复元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
d = [(1,0),(2,3),( 3,2),(0,1)]
OutputRequired = [(1,0),(2,3)]输出顺序无关紧要p>
命令set()无法正常工作。
解决方案
这个解决方案,我检查它是否已经存在于 temp
中,然后将每个元组复制到 temp
中然后复制回 d
。
d = [(1,0 ),(2,3),(3,2),(0,1)]
temp = []
for a,b in d:
if(a,b)not在temp和(b,a)不在temp:#检查重复的元组
temp.append((a,b))
d = temp * 1 #copy temp to d
这将按预期给出输出。
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.
解决方案
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.
这篇关于在嵌套列表Python中删除具有相同元素的重复元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!