去重
作者:互联网
# 公司员工管理,创建一个类,类中的对象属性有:姓名,性别,年龄,职位,假设公司有600名元,通过判断名字,性别来把重复的员工去掉
class Company:
def __init__(self,name,sex,age,partment):
self.name = name
self.sex = sex
self.age = age
self.partment = partment
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
company_list = []
for i in range(200):
company_list.append(Company('alxe','男',i,'python'))
for i in range(200):
company_list.append(Company('wusir','男',i,'python'))
for i in range(200):
company_list.append(Company('taibai','男',i,'python'))
company_set = set(company_list)
for person in company_set:
print(person.__dict__)
标签:,__,name,self,list,sex,company 来源: https://www.cnblogs.com/llzh/p/16339710.html