比较列表中对象的属性列表

时间:2016-10-06 16:46:13

标签: python

这就是我的输入的样子:

cust1Attributes = [{'Key': 'FirstName', 'Value': 'SomeFirstName'}, {'Key': 'LastName', 'Value': 'SomeLastName'}]

这是我的MandatoryData列表的样子:

class MustHaveData(object):
    def __init__(self, name, defaultValue):
        self.name = name
        self.defaultValue = defaultValue

customerMandatoryData=[]
customerMandatoryData.append(MustHaveData(name="FirstName", defaultValue="Default First Name"))
customerMandatoryData.append(MustHaveData(name="LastName", defaultValue="Default Last Name"))
customerMandatoryData.append(MustHaveData(name="State", defaultValue="Default State"))

我需要将cust1Attributes's keycustomerMandatoryData's name进行比较,并获取customerMandatoryData的{​​{1}}列表,而不是cust1Attributes

我该怎么做?

1 个答案:

答案 0 :(得分:2)

从包含每个custset = {x['Key'] for x in cust1Attributes} result = [obj for obj in customerMandatoryData if obj.name not in custset] 的项目的字典中构建,并使用列表解析过滤掉集合中名称的对象:

distance.execute(graphModel, attributeModel);