{
UUID('a639efb6-d4e0-4929-b309-8171bf61b508'):
[UUID('d0576830-df96-4411-ac33-4bac597f2010'),
UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'),
UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')],
UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'):
[UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')]
}
-------
[
{u'count': 1, u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')},
{u'count': 17, u'_id': UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')},
{u'count': 1, u'_id': UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa')},
{u'count': 1, u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010')}
]
第一个词典具有帐户UUID,作为与该帐户所属的计算机组UUID数组关联的键。 该数组包含字典,该字典给出每个计算机组UUID的计算机数。
我想要的是每个帐户的计算机数量。我怎样才能将两个数据集结合起来呢?
答案 0 :(得分:0)
所以请确保我理解:您有两个字典。第一个映射从cloud_account映射到machine_groups列表。第二个从machine_group映射到计算机列表。您想要一个从cloud_account到总计算机数的映射。此代码段说明了如何通过简单的理解来做到这一点。
foo = {'A': ['foo', 'bar'], 'B': ['baz']}
bar = {'foo': ['a', 'b', 'c'], 'bar': ['d'], 'baz': ['e', 'f']}
baz = {k: sum(len(bar[k2]) for k2 in v) for k, v in foo.items()}
print(baz)
打印:
{'A': 4, 'B': 2}
答案 1 :(得分:0)
您的字典我将称为account_dict
,您的数组将我称为group_array
。
为了加快速度(特别是如果您必须执行很多操作),我首先建议创建计算机组UUID到计算机计数的映射:
group_count = {mg[u'_id']:mg[u'count'] for mg in group_array}
现在有了这个,您可以按以下方式创建帐户到计算机总数的映射:
account_machine_count = {account: sum([group_count[group] for group in account_dict[account]])
for account in account_dict}
sum([group_count[group] for group in account_dict[account]])
将遍历account_dict
中每个帐户的所有组,在我们创建的group_count
字典中查找其计算机数,并将它们全部加在一起。
答案 2 :(得分:0)
只是写了很长时间来澄清我从您的描述中得到了什么(希望对您有帮助)
from uuid import UUID
groups = {UUID('a639efb6-d4e0-4929-b309-8171bf61b508'):
[UUID('d0576830-df96-4411-ac33-4bac597f2010'),
UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'),
UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520')],
UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'): [UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')]}
group_machines = [{u'count': 1, u'_id': UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a')},
{u'count': 17, u'_id': UUID(
'c0a36d2e-f7dd-4d62-abe8-c26249e52520')},
{u'count': 1, u'_id': UUID(
'a650e642-ee41-4ab7-8ad1-797c7bdcd2aa')},
{u'count': 1, u'_id': UUID('d0576830-df96-4411-ac33-4bac597f2010')}]
result_detail = {}
result_sum = {}
for group_id, machines_id in groups.items():
for machine_id in machines_id:
machine_count = tuple(
filter(lambda m_details: m_details['_id'] == machine_id, group_machines))
if machine_count:
if group_id not in result_detail:
result_sum[group_id] = machine_count[0][u'count']
result_detail[group_id] = [
{machine_id: machine_count[0][u'count']}]
else:
result_sum[group_id] = result_sum[group_id] + \
machine_count[0][u'count']
result_detail[group_id].append(
{machine_id: machine_count[0][u'count']})
print(result_detail)
#{UUID('a639efb6-d4e0-4929-b309-8171bf61b508'): [{UUID('d0576830-df96-4411-ac33-4bac597f2010'): 1}, {UUID('a650e642-ee41-4ab7-8ad1-797c7bdcd2aa'): 1}, {UUID('c0a36d2e-f7dd-4d62-abe8-c26249e52520'): 17}], UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'): [{UUID('31140d2e-15a1-4203-bdbb-55c14cd6b83a'): 1}]}
print(result_sum)
#{UUID('a639efb6-d4e0-4929-b309-8171bf61b508'): 19, UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'): 1}
缩影:
from functools import reduce
result = {group_id: reduce(lambda s, m_detail: s + m_detail[u'count'],
filter(lambda m_detail: m_detail[u'_id'] in machines_id,
group_machines),
0)
for group_id, machines_id in groups.items()}
print(result)
#{UUID('a639efb6-d4e0-4929-b309-8171bf61b508'): 19, UUID('fd4bbf86-9803-4724-9576-d83355bdcff2'): 1}