在下面的代码中,我试图找到重复的对象并将其分配给原始对象列表.是否有一种优化的方法来做同样的事情
user_list = []
unique_user = []
for user in users:
if user.id not in user_list:
user_list.(user.id)
unique_user.append(user)
users = unique_user
Run Code Online (Sandbox Code Playgroud)
编辑
users = [<User {u'username': u'rr', u'name': u'rr', u'enabled': True, u'tenantId': u'81ec14658b764c6799871b9e5573e76f', u'id': u'6bdf7afb1d2e4bd19f7d807063720ac3', u'email': u'r@r.com'}>, <User {u'username': u'rr', u'name': u'rr', u'enabled': True, u'tenantId': u'81ec14658b764c6799871b9e5573e76f', u'id': u'6bdf7afb1d2e4bd19f7d807063720ac3', u'email': u'r@r.com'}>, <User {u'username': u'y', u'name': u'y', u'enabled': True, u'tenantId': u'81ec14658b764c6799871b9e5573e76f', u'id': u'd4afeeb8bc554f2083f93f68638dac0d', u'email': u'y@y.com'}>]
Run Code Online (Sandbox Code Playgroud)
users = set(users)
Run Code Online (Sandbox Code Playgroud)
这将可迭代变成一组无序的独特元素.文件.
对象需要可以清洗:
class A(object):
def __init__(self, a):
self.a = a
def __eq__(self, other):
return self.a == other.a
def __hash__(self):
return hash(self.a)
set([A(2), A(2), A(1)])
{<__main__.A at 0x2676090>, <__main__.A at 0x2676110>}
Run Code Online (Sandbox Code Playgroud)
编辑: 如何使它们可以清洗
for user in users:
user.__eq__ = lambda self, other: self.id == other.id
user.__hash__ = lambda self: hash(self.id)
unique_users = set(users)
# If you want to remove the attributes to leave them as they were:
unique_users = [delattr(usr, __hash__) for usr in unique_users]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |