如何組合具有不同字段的數組

[英]How to combine to arrays with diffrent fields


{
    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')}
]

The first dictionary has account UUIDs as keys associated with arrays of machine group UUIDs that the account belongs to. The array contains dictionaries that give the number of machines for each machine group UUID.

第一個字典將帳戶UUID作為與帳戶所屬的計算機組UUID陣列相關聯的密鑰。該數組包含為每個計算機組UUID提供計算機數量的字典。

What I would like to have is the number of machines per account. How can I combine the two data sets to get that?

我想要的是每個帳戶的機器數量。如何組合這兩個數據集來實現呢?

3 个解决方案

#1


0  

So just to be make sure I understand: you've got two dictionaries. The first maps from cloud_account to a list of machine_groups. The second maps from machine_group to a list of machines. You would like a map from cloud_account to number of total machines. This snippet illustrates how to do that with a simple comprehension.

所以只是為了確保我理解:你有兩本詞典。第一個映射從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)

prints:

{'A': 4, 'B': 2}

#2


0  

Your dictionary I will refer to as account_dict, your array I will refer to as group_array.
To speed this up (especially if you have to do a lot of these), I first recommend creating a mapping of machine group UUIDs to machine counts:

您將我的字典稱為account_dict,我將您的數組稱為group_array。為了加快速度(特別是如果你必須做很多這些),我首先建議創建一個機器組UUID到機器計數的映射:

group_count = {mg[u'_id']:mg[u'count'] for mg in group_array}

Now that you have this, you can create your mapping of accounts to total machine count in the following manner:

現在您已經擁有了這個,您可以通過以下方式創建帳戶到總計算機數量的映射:

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]]) will go through all the groups in account_dict for each account, look up their machine count in the group_count dict we created, and add them all together.

sum(account_dict [account]中的group的[group_count [group])將遍歷account_dict中所有組的每個帳戶,在我們創建的group_count dict中查找他們的計算機數量,並將它們全部加在一起。

#3


0  

Just wrote long to clarify what I got from your descriptions (Hope this help you):

剛寫了很長時間來澄清我從你的描述中得到的東西(希望這對你有所幫助):

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}

Shorter:

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}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/06/28/72f267ba3583675de0035b7f31fc9d05.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com