添加键中缺少值的字典值 [英] Adding dictionary values with the missing values in the keys

查看:146
本文介绍了添加键中缺少值的字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个词典:

Mydict = {'(1)': 850.86,
          '(1, 2)': 1663.5,
          '(1, 2, 3)': 2381.67,
          '(1, 3)': 1568.89,
          '(2)': 812.04,
          '(2, 3)': 1529.45,
          '(3)': 717.28}

A = {1: 4480.0, 2: 3696.0, 3: 4192.5}
B = {1: 1904.62, 2: 1709.27, 3: 1410.73}

基于Mydict中的键,我想添加min(A, B)缺少的键值.例如,对于Mydict中的第一个键'(1)',我想将min(A[2], B[2]) + min(A[3], B[3])添加到第一行的值并更新该字典中的值.同样,对于键'(1, 2)'的值,我想添加min(A[3] + B[3]),因为那里仅缺少3.对于'(1, 2, 3)',我不需要添加任何内容,因为它涉及所有3个数字,即1、2和3.因此,我的新Mydict将如下所示:

Based on the keys in Mydict, I want to add the missing key value of min(A, B). For example, For the first key '(1)' in Mydict, I want to add min(A[2], B[2]) + min(A[3], B[3]) to the value of the first row and update the value in that dictionary. Similarly, for the value of the key: '(1, 2)', I want to add the min(A[3] + B[3]) as only 3 is missing in there. For '(1, 2, 3)', I don't need to add anything as it involves all the 3 numbers, namely 1, 2, and 3.Thus, my new Mydict will be as following:

Mynewdict = {'(1)': 3970.86,
              '(1, 2)': 3074.23,
              '(1, 2, 3)': 2381.67,
              '(1, 3)': 3278.16,
              '(2)': 4127.39,
              '(2, 3)': 3434.07,
              '(3)': 4331.17}

在此示例中,B的所有值都小于A的值,但是,可能并非一直如此,这就是为什么我要添加其中的最小值.感谢您的回答.

In this example, all the values of B are less than the values of A, however, it may not be the case for all the time, that is why, I want to add the minimum of those. Thanks for answers.

推荐答案

数字列表听起来是个好主意,但对我来说,它仍然需要与以下解决方案相似的(类似)操作数量:

The list of numbers sounds like a good idea, but to me it seems it will still require similar amount of (similar) operations as the following solution:

import re

str_pat = re.compile(r'\((.*)\)')

Mynewdict = Mydict
for key in Mynewdict.keys():

    match = (str_pat.findall(key)[0]).split(',')

    # set(list(map(int, match))) in Python 3.X
    to_add = list(set(A.keys()).difference(set(map(int,match))))

    if to_add:
        for kAB in to_add:
            Mynewdict[key] += min(A[kAB],B[kAB])

对于Mynewdict中的每个键,该程序都会在括号之间找到模式,并将其转换为列表match除以,.然后将此列表与A中的键列表进行比较.

For each key in Mynewdict this program finds the pattern between the brackets and turns it into a list match split by ,. This list is then compared to list of keys in A.

比较通过集合-程序构造来自两个列表的集合,并将集合差异(也变成列表)返回到to_add中.因此,to_add是在A中具有键的列表,这些键是在Mynewdict中的复合键中不存在的数字.假定B中的所有键也都出现在A中. map用于将match中的字符串转换为整数(用于与A中的整数键进行比较).要在Python 3.X中使用它,您还需要将map(int, match)变成注释中所述的列表.

The comparison goes through sets - the program construct sets from both lists and returns a set difference (also turned into list) into to_add. to_add is hence a list with keys in A that are numbers not present in the compound key in Mynewdict. This assumes that all the keys in B are also present in A. map is used to convert the strings in match to integers (for comparison with keys in A that are integers). To use it in Python 3.X you need to additionally turn the map(int, match) into a list as noted in the comment.

程序的最后部分为每个丢失的键将A和B之间的最小值分配给Mynewdict的现有值.由于Mynewdict最初是Mydict的副本,因此所有最终键和初始值已存在于其中,因此该程序无需检查键是否存在或显式添加初始值.

Last part of the program assigns minimum value between A and B for each missing key to the existing value of Mynewdict. Since Mynewdict is initially a copy of Mydict all the final keys and intial values already exist in it so the program does not need to check for key presence or explicitly add the initial values.

要在Mynewdict中查找与特定值相对应的键,看来您实际上必须遍历字典:

To look for keys in Mynewdict that correspond to specific values it seems you actually have to loop through the dictionary:

find_val = round(min(Mynewdict.values()),2)
for key,value in Mynewdict.items():
    if find_val == round(value,2):
        print key, value

这里重要的是四舍五入.因为Mynewdict中的值具有可变精度,所以该精度通常比您要查找的值的精度更长,这是必要的.换句话说,没有round(value,2)的if在实际为True的情况下将计算为False.

What's important here is the rounding. It is necessary since the values in Mynewdict have variable precision that is often longer than the precision of the value you are looking for. In other words without round(value,2) if will evaluate to False in cases where it is actually True.

这篇关于添加键中缺少值的字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆