Python - 返回唯一的对象列表

时间:2016-05-26 16:11:40

标签: python

我正在尝试获取一个唯一的对象列表,我有一些代码可以从API中提取数据,然后将这些数据放入一个对象中。然后我将这些对象放在一个列表中。但是有些对象是重复的,我想知道如何删除它们?

样本列表数据:

[
Policy: 'SQL', 
SecondaryPolicy: 'ORACLE', 
Level: 'Primary On Call Engineer',
LevelNo: 1, 
StartDate: None, 
EndDate: None, 
StartTime: None, 
EndTime: None, 
Name: 'Fred', 
Mobile: '123', 

Policy: 'Comms', 
SecondaryPolicy: '', 
Level: 'Primary On Call Engineer',
LevelNo: 1, 
StartDate: None, 
EndDate: None, 
StartTime: None, 
EndTime: None, 
Name: 'Bob', 
Mobile: '456', 

Policy: 'Infra', 
SecondaryPolicy: '', 
Level: 'Primary On Call Engineer',
LevelNo: 1, 
StartDate: None, 
EndDate: None, 
StartTime: None, 
EndTime: None, 
Name: 'Bill', 
Mobile: '789', 

Policy: 'Comms', 
SecondaryPolicy: '', 
Level: 'Primary On Call Engineer',
LevelNo: 1, 
StartDate: None, 
EndDate: None, 
StartTime: None, 
EndTime: None, 
Name: 'Bob', 
Mobile: '456', 
]

代码(香港专业教育学院删除了一些对象数据并输入了样本数据,对于这个测试,我只是试图让freds结果返回一次)

objPolicyData = getUserData()

OnCallData = [] 
for UserItem in objPolicyData['users']:   
    UserData = User()     
    #get the user object from DB
    UserData.Name   = 'Fred'
    for OnCall in UserItem['on_call']:    
        UserPolicy = OnCall['escalation_policy'] 
        UserData.Policy          = 'SQL'
        UserData.SecondaryPolicy = 'ORACLE'
        OnCallData.append(UserData)

的尝试: 我试过这个

clean_on_call_data = {User.Name for User in OnCallData}

但这仅打印

set(['Fred'])

对象中的其他字段在哪里,我将如何迭代它?

编辑:这是我的班级,cmp是正确的吗?我该如何删除副本?

class User(object):
    __attrs = ['Policy','SecondaryPolicy','Name']

    def __init__(self, **kwargs):
        for attr in self.__attrs:
            setattr(self, attr, kwargs.get(attr, None))

    def __repr__(self):
        return ', '.join(
            ['%s: %r' % (attr, getattr(self, attr)) for attr in self.__attrs])  

    def __cmp__(self):     
        if self.Name != other.Name:  

3 个答案:

答案 0 :(得分:1)

适用于Python 2.x

我认为您希望为存储API数据的类实现__cmp__

适用于Python 3.x

我认为您希望为存储API数据的类实现__eq__

无论使用哪个版本的Python,您都可以使用比较器/ eq方法检查列表中的重复项。如果您定义了set(list),则可以使用__eq__来完成此操作。由于集合是唯一对象的列表。

答案 1 :(得分:0)

如何使用词典,然后使用pandas.DataFrame

类似的东西:

d1 = {
'Policy': 'SQL', 
'SecondaryPolicy': 'ORACLE', 
'Level': 'Primary On Call Engineer',
'LevelNo': 1, 
'StartDate': None, 
'EndDate': None, 
'StartTime': None, 
'EndTime': None, 
'Name': 'Fred', 
'Mobile': '123', 
}
d2 = {
'Policy': 'Comms', 
'SecondaryPolicy': '', 
'Level': 'Primary On Call Engineer',
'LevelNo': 1, 
'StartDate': None, 
'EndDate': None, 
'StartTime': None, 
'EndTime': None, 
'Name': 'Bob', 
'Mobile': '456', 
}
d3 = {
'Policy': 'Infra', 
'SecondaryPolicy': '', 
'Level': 'Primary On Call Engineer',
'LevelNo': 1, 
'StartDate': None, 
'EndDate': None, 
'StartTime': None, 
'EndTime': None, 
'Name': 'Bill', 
'Mobile': '789', 
}
d4 = {
'Policy': 'Comms', 
'SecondaryPolicy': '', 
'Level': 'Primary On Call Engineer',
'LevelNo': 1, 
'StartDate': None, 
'EndDate': None, 
'StartTime': None, 
'EndTime': None, 
'Name': 'Bob', 
'Mobile': '456', 
}


data = pd.DataFrame([d1,d2,d3,d4])

data[ data.Name=='Fred' ]

哪些出局:

enter image description here

答案 2 :(得分:0)

您可以继承User类并实施__eq____hash__方法,然后将其添加到set,如下所示:

class UserUnique(User):
    def __hash__(self):
        return hash(self.Name)
    def __eq__(self, o):
        return self.Name == o.Name

然后你可以这样做:

OnCallData = set()
for UserItem in objPolicyData['users']:   
    UserData = UserUnique()     
    UserData.Name = 'Fred'
    for OnCall in UserItem['on_call']:    
        UserPolicy = OnCall['escalation_policy'] 
        UserData.Policy = 'SQL'
        UserData.SecondaryPolicy = 'ORACLE'
        OnCallData.add(UserData)
相关问题