将一个嵌套字典中的选择项追加到另一个

问题描述 投票:0回答:1

我希望将列表项从一个嵌套字典追加到另一个(从SystemDicto3到SystemDictu4)。它们都具有相同类型的内容,系统编号(密钥)和哪些电子邮件(值)属于该系统编号。到目前为止,我有以下代码(完整代码片段):

#Gets the key/system with the lowest amount of values/emails, set = to SysMin
SysLength = defaultdict(list)
for x in SystemDict:
    length = len(SystemDict[x])
    SysLength[x].append(length) 
SysMin = min(SysLength, key=SysLength.get)

for system in SystemDicto3: #Iterates through systems
    for item in SystemDicto3[system]: #iterates through emails within systems
        if item in system != SystemDictu4[system]: #if the email isn't in the dict, add it
            SystemDictu4.append(item)

我想迭代SystemDicto3字典电子邮件(dfo3数据框用于创建此字典),根据它所在的系统将内容添加到alreay填充的SystemDictu4字典中,并优先考虑具有最低计数的系统。

例如,如果电子邮件“[email protected]”在(SystemDicto3)中:

 ['System 1']
 ['System 3']
 ['System 5'] 
 ['System 7'] 

我想将该电子邮件附加到三个最低的SystemDictu4系统。因此,如果SystemDictu4中的长度如下:

system 1 = 100 
system 3 = 40 
system 5 = 200 
system 7 = 90

我想将该电子邮件附加到系统1,3和7。

我没有包含我的完整代码,因为它可能会使问题复杂化。任何帮助将不胜感激,并感谢您花时间阅读本文!

字典看起来像下面的代码。每个系统最多可以有60个系统和1000个电子邮件,同一个电子邮件可以显示在不同的系统中,我的目标是将第二个字典中的电子邮件添加到第一个字典中。预期输出将是一个填充的字典,以及一个包含未发送到第一个字典的电子邮件的字典。

    {'System 1':                         [email protected]
                                       [email protected]
                                       [email protected]

'System 2':                                [email protected]
                                           [email protected]
                                           [email protected]

 'System 3':                              [email protected]
                                          [email protected]
                                          [email protected]

'System 4':                                [email protected]
                                           [email protected]
                                           [email protected]   }
python pandas dictionary nested append
1个回答
1
投票

首先,我们将列表字典转换为集合字典。

SystemDictu4 = {'System 1': ['[email protected]', '[email protected]', '[email protected]'],
                'System 2': ['[email protected]', '[email protected]', '[email protected]'],
                'System 3': ['[email protected]', '[email protected]', '[email protected]'],
                'System 4': ['[email protected]', '[email protected]', '[email protected]']}
System = {k,set(v) for k,v in SystemDictu4.items()}

我建议使用一个集合,因为每个系统密钥只需要一个每个电子邮件地址的实例。一旦我们将较大的系统转换为集合字典,我们就可以使用集合的性质来统一来自其他系统的信息。

def add_dict(SystemDictFrom, SystemDictTo, number_of_systems):
    for system in sorted(SystemDictFrom, key = lambda a: len(SystemDictFrom.get(a)))[:number_of_systems]:
        if system in SystemDictTo:
            SystemDictTo[system].union(SystemDictFrom[system])
        else:
            SystemDictTo[system] = SystemDictFrom[system]

在上面的函数中,我使用SystemDictFrom作为列表或集合的字典,它将向更大的字典SystemDictTo添加值。

您可以使用以下命令调用该函数:

# This will add the three least populated systems from SystemDicto3 to SystemDictu4
add_dict(SystemDicto3, SystemDictu4, 3)
© www.soinside.com 2019 - 2024. All rights reserved.