重复对象的python过滤列表

时间:2014-05-23 10:18:07

标签: python

在下面的代码中我试图找到重复的对象并将其分配给原始对象列表。是否有一种优化的方法来做同样的事情

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

修改

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'}>]

1 个答案:

答案 0 :(得分:5)

users = set(users)

将可迭代变为无序的唯一元素集。 Docs

对象必须是hashable

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>}

修改 如何使它们可以播放

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]