细分嵌套字典,同时应用Python中值的百分比

时间:2015-06-19 03:27:50

标签: python dictionary

我想细分字典,同时将值(百分比)应用于另一个字符的值。

我有这些数据集:

{'C-STD-B&M-SUM': {datetime.date(2015, 5, 20): 0.21484699999999998,
               datetime.date(2015, 5, 21): 0.245074,
               datetime.date(2015, 5, 22): 0.27874}

{'G-CAM-BAC-SUM': {datetime.date(2015, 5, 20): 0.13294399999999998,
               datetime.date(2015, 5, 21): 0.151648,
               datetime.date(2015, 5, 22): 0.17248,
               datetime.date(2015, 5, 23): 0.195664}

{'G-CAM-BAC-XS': 0.06, 'G-CAM-BAC-XXS': 0.01, 'G-CAM-BAC-XL': 0.11, 'G-CAM-BAC-S': 0.19, 'G-CAM-BAC-L': 0.26, 'G-CAM-BAC-XXL': 0.03, 'G-CAM-BAC-M': 0.35}

{'C-STD-B&M-XL': 0.3, 'C-STD-B&M-XXL': 0.11, 'C-STD-B&M-S': 0.06, 'C-STD-B&M-M': 0.2, 'C-STD-B&M-XS': 0, 'C-STD-B&M-L': 0.32}

预期产出:

{'C-STD-B&M-XL': {datetime.date(2015, 5, 20): 0.21484699999999998*0.3,
           datetime.date(2015, 5, 21): 0.245074*0.3,
           datetime.date(2015, 5, 22): 0.27874*0.3}

{'C-STD-B&M-XXS': {datetime.date(2015, 5, 20): 0.21484699999999998*0.1,
           datetime.date(2015, 5, 21): 0.245074*0.1,
           datetime.date(2015, 5, 22): 0.27874*0.1}

{'C-STD-B&M-XXL': {datetime.date(2015, 5, 20): 0.21484699999999998*0.11,
           datetime.date(2015, 5, 21): 0.245074*0.11,
           datetime.date(2015, 5, 22): 0.27874*0.11}

等等,对于所有词典。请注意,我需要对值进行乘法运算的结果,而不是语句,只需将它们保留为更清晰。

到目前为止我的代码(部分):

def apply_size_distribution(dictionary_with_temporal_distribution):
    gown_cap_size = get_size_distribution('G2:G7', 'H2:H7')
    cap_medium_demand = gown_cap_size['C-STD-B&M-M']
    for k, v in dictionary_with_temporal_distribution.items():
        if k == "C-STD-B&M-SUM":
            dictionary_with_temporal_distribution['C-STD-B&M-M'] = dictionary_with_temporal_distribution.pop('C-STD-B&M-SUM')
            for k, v in dictionary_with_temporal_distribution['C-STD-B&M-M'].items():
                dictionary_with_temporal_distribution["{}".format(k)] = v * cap_medium_demand

但是我在迭代期间改变了旧的字典。此外,使用我的代码我将不得不复制和粘贴每个大小的代码,只将键的名称更改为适当的大小。我想知道是否有更强大的方法。

1 个答案:

答案 0 :(得分:0)

您的dictionary_with_temporal_distribution已更改,因为在您的代码中您正在更改它 -

dictionary_with_temporal_distribution["{}".format(k)] = v * cap_medium_demand

相反,您应该考虑在开始时创建一个新词典,并在您继续处理时添加元素,最后返回它。

此外,您应该使用gown_cap_size['C-STD-B&M-M']找出公共部分 - k == "C-STD-B&M-SUM",然后使用string.rsplit('-', 1)[0],而不是像您一样对值进行硬编码 - C-STD-B&M - def apply_size_distribution(dictionary_with_temporal_distribution): gown_cap_size = get_size_distribution('G2:G7', 'H2:H7') retlist = {} for cap_demand in grown_cap_size: for k, v in dictionary_with_temporal_distribution.items(): if k.rsplit('-', 1)[0] == cap_demand.rsplit('-', 1)[0]: if cap_demand not in retlist: retlist[cap_demand] = {} temp = retlist[cap_demand] for k1, v1 in dictionary_with_temporal_distribution[k].items(): temp[k1] = v1 * grown_cap_size[cap_demand] return retlist 为了平等。

您的代码看起来像 -

bq query --allow_large_results --destination_table=project:DATASET.table "SELECT * FROM [project:DATASET.another_table]"