zoukankan      html  css  js  c++  java
  • python 字典的减法

    da = [
        {"a": 231, "b": 456},
        {"a": 423, "b": 980},
        {"a": 846, "b": 1960},
        {}
    ]
    lst = []
    for i in range(len(da) - 1):  # 0 1
        value1 = list(da[i].values())
        value2 = list(da[i + 1].values())
        if value1 and value2:
            for j in range(len(value1)):
                lst.append(value1[j] - value2[j])
    print(lst)
    ##########结果
    [-192, -524, -423, -980]
    
    da = [
        {"a": 231, "b": 456},
        {"a": 423, "b": 980},
        {"a": 846, "b": 1960},
        {}
    ]
    lst = []
    for i in range(len(da) - 1):  # 0 1
        value1 = list(da[i].values())
        value2 = list(da[i + 1].values())
        if value1 and value2:
            v_lst = []
            for j in range(len(value1)):
                v_lst.append(value1[j] - value2[j])
            lst.append(v_lst)
    print(lst)
    #############结果
    [[-192, -524], [-423, -980]]
    
    lst = []
    for i in range(len(da) - 1):  # 0 1
        value1 = list(da[i].values())
        value2 = list(da[i + 1].values())
        v_lst = []
        if not value1:
            lst.append(v_lst)
        elif not value2:
            lst.append(v_lst)
        elif value1 and value2:
            for j in range(len(value1)):
                v_lst.append(value1[j] - value2[j])
            lst.append(v_lst)
    print(lst)
    ################结果
    [[], [-192, -524], [], [], []]
    

    这种稍微麻烦一点 需要给每个字典的外层在加一层列表好用索引来

    this_month_list.append(this_month_cumulative_dic)
    this_month_cumulative_list.append(this_month_list)  # 各个指标的本月止累计 总和
    same_month_list.append(same_month_cumulative_dic)
    same_month_cumulative_list.append(same_month_list)  # 各个指标去年同月止累计 总和
    for m in range(12):
        for base_dict, sub_dict in zip(this_month_cumulative_list[m], this_month_cumulative_list[m + 1]):
            this_month.append({k: v - sub_dict.get(k, 0) for k, v in base_dict.items()})
    
    

    那个满足就用那个吧,能实现功能就行

  • 相关阅读:
    tail命令和head命令查询文件指定行数
    php unserialize(): Error at offset 470 of 660 bytes
    屏蔽搜索引擎收录robots.txt文件下载
    php-apc为magento加速
    magento导入csv文件到数据库中乱码
    magento 上传csv表格中实例化对象例子
    magento上新产品,前台不显示不显示属性,后台却有属性问题
    php裁剪图片
    概念:静态static相关知识
    概念 : 重定向概念
  • 原文地址:https://www.cnblogs.com/zgboy/p/14791986.html
Copyright © 2011-2022 走看看