在python中迭代地将列表和检查与下一个列表组合

在python中迭代地将列表和检查与下一个列表组合,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有多本字典和一本基本字典。我需要迭代字典键,并需要在每次迭代中检查现有键和新键,但在每次迭代中,我需要组合字典的所有先前键,并使用当前字典键进行检查 dict10={'A':1,'C':2}#基本字典 dict11={'B':3,'C':4} dict12={'A':5'E':6'F':7} 下面是计算过程 Exist_Score=(dict11.keys()和dict10.keys()中的键值)+(dict12.keys()和(dict11.keys()+dict10.keys())中的

我有多本字典和一本基本字典。我需要迭代字典键,并需要在每次迭代中检查现有键和新键,但在每次迭代中,我需要组合字典的所有先前键,并使用当前字典键进行检查


dict10={'A':1,'C':2}#基本字典
dict11={'B':3,'C':4}
dict12={'A':5'E':6'F':7}
下面是计算过程

Exist_Score=(dict11.keys()和dict10.keys()中的键值)+(dict12.keys()和(dict11.keys()+dict10.keys())中的键值)

New_score=(dict11.keys()-dict10.keys()中的键值)+(dict12.keys()中的键值-(dict11.keys()+dict10.keys()))

我手工计算分数的方法

exist\u得分=0
新评分=0
对于dict11.keys()和dict10.keys()中的键:
exist_分数+=dict11[关键点]
对于dict12.keys()和set(dict11.keys()).union(set(dict10.keys())中的键:
exist_分数+=12[关键点]
对于dict11.keys()中的键-dict10.keys():
新的分数+=dict11[关键]
对于dict12.keys()-set(dict11.keys()).union(set(dict10.keys())中的键:
新的分数+=12[关键点]
打印(存在分数)
打印(新分数)
对于给定的示例,分数将为

存在_分数=4+5

新的大学分数=3+(6+7)


如何实现动态列表和迭代组合列表以检查键的目的?

使用提供的字典,即

dict10 = {'A':1, 'C':2} #base dictionary
dict11 = {'B':3, 'C':4}
dict12 = {'A':5, 'E':6, 'F':7}
我们找到与基本字典相同的键和不在基本字典中的键,然后使用某些组合字典中的键计算分数:

# Combine all three dictionaries into one
dict_combine = dict(dict10, **dict(dict11, **dict12))

# Find keys that are the same with the base dictionary
keys_same = (dict10.keys() & dict11.keys()) | (dict10.keys() & dict12.keys())

# Find keys that are not in the base dictionary
keys_new = dict10.keys() ^ dict11.keys() ^ dict12.keys()

# Calculate scores
exist_score = sum([dict_combine[key] for key in keys_same])
new_score = sum([dict_combine[key] for key in keys_new])
现在,分数是我们所期望的:

>> exist_score
9
>> new_score
16
可以根据需要扩展代码以获得更多词典


编辑:

如果非基本字典不一定具有唯一键(即它们可能彼此共享同一键),则可以根据您提供的更新代码使用此功能:

def get_scores(base_dict, *new_dicts):
    # Initialize scores
    exist_score = 0
    new_score = 0

    # Iterate through non-base dictionaries
    for i, i_dict in enumerate(new_dicts):
        # Get base dictionary keys and find unions
        keys = base_dict.keys()
        for j in range(i):
            keys = keys | new_dicts[j].keys()

        # Find keys for existing score and new score
        keys_same = i_dict.keys() & keys
        keys_new = i_dict.keys() - keys

        # Update scores
        exist_score += sum([i_dict[key] for key in keys_same])
        new_score += sum([i_dict[key] for key in keys_new])

    # Return scores as tuple
    return exist_score, new_score
现在,当我使用您的词典运行它时,我得到以下结果:

>> exist_score, new_score = get_scores(dict10, dict11, dict12)
>> exist_score
9
>> new_score
16

这将适用于动态数量的字典,使用函数头中的
*args
符号。请参阅。

存在的\u分数
新的\u分数
是使用非python代码的内容计算的。因此,我不知道您试图计算什么,因此无法帮助扩展此功能抱歉,这不是python代码我刚才解释了计算分数应该如何实现前两个词典的共同点是C,它对于dict10和dict11具有不同的值。在所有这些操作中,是否始终要获取第二个dict中字典的值?@alkasm我始终获取当前字典的值。例如,如果我正在学习dict11,那么我将学习该dict中的值。您如何检索exist_分数的4和5的值。它似乎取了dict10和dict11的交集内的最大值+dict12和(dict11+dict10)的并集的交集内的最大值?谢谢,但我不只是用基本字典检查键。我已经更新了代码,您可以检查。@kumaranragunathan啊,所以不能保证dict11的键与dict12的键不同?@kumaranragunathan我添加了一个函数,以更广义的方式与您的匹配!