add object into python#39;s set collection and determine by object#39;s attribute(将对象添加到python的集合中并通过对象的属性确定)
问题描述
我有一个像这样的 Person 类:
I have a Person class like this:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __repr__(self):
        return '<Person {}>'.format(self.name)
我想将此类的一些实例添加到集合中,如下所示:
I want to add some instances of this class to a set, like this:
tom = Person('tom', 18)
mary = Person('mary', 22)
mary2 = Person('mary2', 22)
person_set = {tom, mary, mary2}
print(person_set)
# output: {<Person tom>, <Person mary>, <Person mary2>}
如您所见,集合中有 2 个玛丽.如何使具有相同年龄的 Person 实例被视为同一个人,并且只添加到集合中一次?
As you can see, there are 2 Marys in the set. How can I make it so that Person instances with the same age are considered the same person, and only added to the set once?
换句话说,我怎样才能得到 {<Person tom>, <Person mary>} 的结果?
In other words, how can I get a result of {<Person tom>, <Person mary>}?
推荐答案
当一个新对象被添加到一个python集合中时,首先计算对象的哈希码,然后,如果一个或多个对象具有相同的哈希值代码已经/已经在集合中,这些对象将被测试是否与新对象相等.
When a new object is being added to a python set, the hash code of the object is first computed and then, if one or more objects with the same hash code is/are already in the set, these objects are tested for equality with the new object.
这样做的结果是你需要实现 __hash__(...) 和 __eq__(...) 类的方法.例如:
The upshot of this is that you need to implement the __hash__(...) and __eq__(...) methods on your class. For example:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __eq__(self, other):
        return self.age == other.age
    def __hash__(self):
        return hash(self.age)
    def __repr__(self):
        return '<Person {}>'.format(self.name)
tom = Person('tom', 18)
mary = Person('mary', 22)
mary2 = Person('mary2', 22)
person_set = {tom, mary, mary2}
print(person_set)
# output: {<Person tom>, <Person mary>}
但是,您应该非常仔细地考虑 __hash__ 和 __eq__ 的正确实现对于您的类应该是什么.上面的示例有效,但没有意义(例如,__hash__ 和 __eq__ 都仅根据年龄定义).
However, you should think very carefully about what the correct implementation of __hash__ and __eq__ should be for your class. The above example works, but is non-sensical (e.g. in that both __hash__ and __eq__ are defined only in terms of age).
这篇关于将对象添加到python的集合中并通过对象的属性确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将对象添加到python的集合中并通过对象的属性确定
 
				
         
 
            
        基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				