如何在python中删除重复的字段并从中创建子列表?

时间:2018-06-28 10:01:27

标签: python

[{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010')}, 

{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa')}, 

{u'cloud_account': UUID('a639efb6-d4e0-4929-b309-8171bf61b508'), u'_id': UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')}, 

{u'cloud_account': UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'), u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')}])

“ _ id”字段是指machine_groups的ID,我想有一个子列表,用于将第一个云帐户的ID重新组合。

1 个答案:

答案 0 :(得分:0)

每个云帐户的计算机组列表

类似的事情可能会有所帮助:

cloud_accounts = [
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'd0576830-df96-4411-ac33-4bac597f2010'},
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'},
    {'cloud_account': 'a639efb6-d4e0-4929-b309-8171bf61b508', '_id': 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'},
    {'cloud_account': 'fd4bbf86-9803-4724-9576-d83355bdcff2', '_id': '31140d2e-15a1-4203-bdbb-55c14cd6b83a'}]

machine_groups_per_cloud_account = {d['cloud_account']: [] for d in cloud_accounts}
for d in cloud_accounts:
    machine_groups_per_cloud_account[d['cloud_account']].append(d['_id'])

import pprint
pprint.pprint(machine_groups_per_cloud_account)

以下是输出:

{'a639efb6-d4e0-4929-b309-8171bf61b508': ['d0576830-df96-4411-ac33-4bac597f2010', 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa', 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'], 
 'fd4bbf86-9803-4724-9576-d83355bdcff2': ['31140d2e-15a1-4203-bdbb-55c14cd6b83a']}

在您给出的结果示例中,您在dict中多次使用相同的键,这是不正确的,因此这里的想法是将cloud_account视为python dict键并附加{{1 }}关联列表中(python dict值)。

每个云帐户的计算机数量

_id

以下是输出:

machine_groups = [
    {'count': 1, '_id': '31140d2e-15a1-4203-bdbb-55c14cd6b83a'},
    {'count': 17, '_id': 'c0a36d2e-f7dd-4d62-abe8-c26249e52520'},
    {'count': 1, '_id': 'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'},
    {'count': 1, '_id': 'd0576830-df96-4411-ac33-4bac597f2010'}]
machine_groups_reorganized = {}
for m in machine_groups:
    machine_groups_reorganized[m['_id']] = m['count']

machine_count_per_cloud_account = {d['cloud_account']: 0 for d in cloud_accounts}
for d in cloud_accounts:
    machine_count_per_cloud_account[d['cloud_account']] += machine_groups_reorganized[d['_id']]

import pprint
pprint.pprint(machine_count_per_cloud_account)