class Employee:
def __init__(self, name, sex, age, part):
self.name = name
self.sex = sex
self.age = age
self.part = part
def __hash__(self):
return hash('%s%s' % (self.name, self.sex))
def __eq__(self, other):
if self.name == other.name and self.sex == other.sex:
return True
employ_lst = []
for i in range(200):
employ_lst.append(Employee('111', '男', 18, 'python'))
for i in range(200):
employ_lst.append(Employee('222', '男', 18, 'python'))
for i in range(200):
employ_lst.append(Employee('333', '男', 18, 'python'))
# set集合的去重机制:先调用hash,再调用eq,eq只有hash相等的时候才触发
set_employee = set(employ_lst)
for person in set_employee:
print(person.__dict__)
Python:set机制
最新推荐文章于 2022-07-23 15:47:21 发布