如何从字典列表中查找唯一键值以创建新列表

How to find unique key value from list of dictionary to create new list

我有以下列表:

asset_list = 
[ 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-1', 'state': 'Alabama', 'rid': 103, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}, 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-2', 'state': 'Alaska', 'rid': 104, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama', 'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama', 'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-4', 'state': 'Arizona', 'rid': 106, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-5', 'state': 'California', 'rid': 107, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-7', 'state': 'Alabama', 'rid': 108, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}

]

我必须根据上面的列表创建另一个列表,其中 producerstate 是唯一的,因此新列表将如下所示:

[
{'producer': 'HIK', 'state': 'Alabama'},
{'producer': 'HIK', 'state': 'Alaska'},
{'producer': 'TF Inc.', 'state': 'Alabama'},
{'producer': 'TF Inc.', 'state': 'Arizona'},
{'producer': 'TF Inc.', 'state': 'California'},
]

为了解决这个问题,我首先获取所有唯一的生产者名称:

producer_unique_list = []
    for producer in asset_list:
        if producer['producer'] not in producer_unique_list:
            producer_unique_list.append(producer['producer'])

然后将每个资产的生产者与 producer_unique_list 进行比较,仅添加(生产者,状态)唯一的条目:

producer_list = []
    run_once = True
    for ass in asset_list:
        p_d = dict()
        if run_once:
            p_d['producer'] = ass['producer']
            p_d['state'] = ass['state']
            producer_list.append(p_d)
            run_once = False
        else:
            for y in producer_list:
                if ass['producer'] != y['producer'] or ass['state'] != y['state']:
                    p_d = dict()
                    p_d['producer'] = ass['producer']
                    p_d['state'] = ass['state']
                    producer_list.append(y)

但看起来这个逻辑是行不通的。任何人都可以提出一些解决此问题的好方法。谢谢

使用以下代码我能够得到想要的结果:

unique_values_set = set([tuple((entry["producer"], entry["state"])) for entry in asset_list])

unique_dict_list = [{'producer': value[0], 'state': value[1]} for value in unique_values_set]

您可以从基于 producerstate 构建一组元组开始。使用集合将处理重复问题。您可以基于该集合构建字典列表,如下所示:

asset_list = [
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-1', 'state': 'Alabama',
        'rid': 103, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'},
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-2', 'state': 'Alaska',
        'rid': 104, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama',
        'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama',
        'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-4', 'state': 'Arizona',
        'rid': 106, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-5', 'state': 'California',
        'rid': 107, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-7', 'state': 'Alabama',
        'rid': 108, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}

]
s = set()
for a in asset_list:
    s.add((a['producer'], a['state']))
out = [{'producer': i[0], 'state': i[1]} for i in s]
print(out)