python-3.x - 如何从集合中删除对象而不考虑顺序
问题描述
我无法从集合中移除对象。我所做的是,创建一个测试类并在其中存储两个变量。它是一个字符串变量。我需要将我创建的对象存储到一个集合中,以及任何(ta,tb)与(tb,ta)相同的对象。因此,每当我将元组添加到我的集合中时,我都无法删除相反的条件。有没有办法在python中做到这一点?
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
self.variables = [a, b]
def __hash__(self):
return hash((self.a, self.b))
def __eq__(self, other: Test):
return type(self) is type(other) and self.endpoint() == other.endpoint() or
self.endpoint() == other.endpoint()[::-1]
def endpoint(self):
return (self.a, self.b)
T = Test('A','B')
T2 = Test("B",'A")
result = set()
result.add(T)
result.add(T2)
However, result is showing me both the objects in it as opposed to one. Is there a way to fix this? Thanks
解决方案
推荐阅读
- java - Java JSONObject 不同键进入列表
- google-apps-script - 如何使用复选框隐藏 Google 表格中的列
- cognos - 无法安装 IBM cognos Analytics
- c - realloc() : char* 变量的下一个大小无效
- c++ - 如何删除 C++ 列表中迭代器指向的元素?
- python - Discord.py 卡片图片库?
- python - 如何使用 pyspark 在 Hadoop 中读取 parquet 文件、更改数据类型并写入另一个 Parquet 文件
- javascript - 手机摄像头通过p5js访问
- mysql - 如何将查询结果导出到 Windows 机器 MySQL 8.0 中的 csv?
- elixir - 如何在 Phoenix Elixir 中的一张表中创建不同的下拉列表