hash算法的移到面试题
一家公司的员工管理系统,记录员工姓名,性别,年龄,部门,若员工内部转岗 例如python开发转到go开发,产生新的记录,姓名,性别,年龄,新的部门
假设有一千个员工,如果几个员工的姓名和性别相同,那么认为是同一个员工,请对这一千个员工去重
1 class Employee: 2 def __init__(self, name, age, sex, partment): 3 self.name = name 4 self.age = age 5 self.sex = sex 6 self.partment = partment 7 8 def __hash__(self): 9 return hash("%s%s" % (self.name, self.sex)) 10 11 def __eq__(self, other): 12 if self.name == other.name and self.sex == other.sex: 13 return True 14 15 16 employee_list = [] 17 for i in range(250): 18 employee_list.append(Employee('Aqiu', i, 'nan', 'python')) 19 for i in range(250): 20 employee_list.append(Employee('qiu', i, 'nan', 'java')) 21 for i in range(250): 22 employee_list.append(Employee('A', i, 'nv', 'c++')) 23 for i in range(250): 24 employee_list.append(Employee('iu', i, 'nv', 'python')) 25 26 employee_set = set(employee_list) 27 for person in employee_set: 28 print(person.__dict__)