python-2.7 - 删除嵌套列表Python中具有相同元素的重复元组

标签 python-2.7 duplicates

我有一个元组列表,我需要删除包含相同元素的元组。

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/

相关文章:

python - PySide-Qt : Could not initialize OLE (error 80010106)

Python:转换数据集

sql - 在 PostgreSQL 中查找比较 2 个字段的重复项

python-3.x - 如何获取 Pandas Dataframe 中列列表中非重复元素的计数?

r - 在R中仅删除数据帧中的相邻重复项

python - 使用 urllib2 登录网站 - Python 2.7

python-2.7 - 如何使用networkx从文本文件创建图形?

python - many2many 字段上的 Odoo 特定表单 View

mysql - 有没有办法在(列表)中包含来自 MySQL 查询 : select. .from..where..id 的重复项

android - 避免列表重复