【问题标题】:Iteratively combine list and check with next list in python迭代组合列表并检查python中的下一个列表
【发布时间】:2019-07-24 18:37:50
【问题描述】:

我有多个字典和一个基本字典。我需要遍历字典键,并且需要在每次迭代中检查现有键和新键,但是在每次迭代中,我需要组合字典的所有先前键并检查当前字典键,例如


dict10 = {'A':1, 'C':2} #base dictionary
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_score = 0
new_score = 0


for key in dict11.keys() & dict10.keys():
    exist_score += dict11[key]

for key in dict12.keys() & set(dict11.keys()).union(set(dict10.keys())):
    exist_score += dict12[key]

for key in dict11.keys() - dict10.keys():
    new_score += dict11[key]

for key in dict12.keys() - set(dict11.keys()).union(set(dict10.keys())):
    new_score += dict12[key]

print(exist_score)
print(new_score)

对于给定的示例,分数将

Exist_Score = 4 + 5

New_Score = 3 + (6 + 7)

如何针对动态数量的列表实现这一点并迭代组合列表以检查键?

【问题讨论】:

  • Exist_ScoreNew_score 是使用非 Python 代码计算的。因此,我不知道您要计算什么,因此无法帮助扩展此功能
  • 对不起,这不是python代码我只是解释了它应该如何实现计算分数
  • 前两个字典的共同键是C,dict10和dict11的值不同。您是否总是想在所有这些操作中获取第二个字典中的字典值?
  • @alkasm 我总是取当前字典的值。例如,如果我使用 dict11,那么我将使用该 dict 中的值。
  • 您如何检索存在分数的 4 和 5 的值。它似乎是在dict10和dict 11的交集内取最大值+dict12和(dict11+dict10)的并集的交集的最大值?

标签: python python-3.x dictionary


【解决方案1】:

使用提供的字典,即

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 表示法。见use of *args and **kwargs

【讨论】:

  • 谢谢,但我不只是用基本字典检查键。我已经更新了你可以检查的代码。
  • @kumaranragunathan 啊,所以不能保证dict11 的密钥与dict12 不同?
  • @kumaranragunathan 我添加了一个函数来匹配你的,以更通用的方式!
猜你喜欢
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多