我有一类对象:
class Object :
def __init__(self,id,x,y):
self.id = id
self.x = x
self.y = y
对象的id是唯一的,但对于x和y,它们可能相似:
例如,让我们有4个对象:
ob1 = Object(0, 2.7, 7.3)
ob2 = Object(1, 2.7, 7.3)
ob3 = Object(2, 3.2, 4.6)
ob4 = Object(3, 2.7, 7.3)
我只想保留x和y不同的对象,例如这里我只想保留ob1和ob3或ob2和ob3或ob4和ob3
我怎么做呢
谢谢
允许使用技术from here将对象添加到集合中
要在集合中放置对象,对象类需要实现:
代码
用法
输出
如果您不想或由于某种原因无法添加在集合中使用对象所需的方法,则可以使用临时字典查找具有唯一属性集的对象:
结果:
相关问题 更多 >
编程相关推荐