zoukankan      html  css  js  c++  java
  • python 字典dict类型合并(不能错过哦)

    我要的字典的键值有些是数据库中表的字段名, 但是有些却不是, 我需要把它们整合到一起, 因此有些这篇文章.(非得凑够150个字,我也是没有办法,扯一点昨天的问题吧,话说python中的session就只能在requests库中发挥作用?就不能想asp.net中那样存值,然后设置过期时间以便验证?我原本是想在python中找个与asp.net中的cache差不多功能的库,结果,缓存那块python好像就是redis和memcached,mongodb之类的,有倒是有一两个,但是在项目里用不上了,web.py中用webpy自己的session也有问题,不能跨.py调用嘛?后续研究吧)
    程序示例:

    key = ['success', 'dangerous']
    value = ''
    
    # 返回的list
    result_list = []
    index = 0
    while index < 4:
        # 中间字典,存储数据,以及防止append覆盖
        result_dict = {}
        _key = key[0]
        value = str(index)
        result_dict[_key] = value
        index = index + 1
        result_list.append(result_dict)
    index = 0
    return_list = []
    print result_list
    while index < 4:
        # 中间字典,存储数据,以及防止append覆盖
        result_dict = {}
        _key = key[1]
        value = str(index)
        result_dict[_key] = value
        dictMerge = dict(result_list[index].items() + result_dict.items())
        return_list.append(dictMerge)
        index = index + 1
    print return_list

    程序输出:

    当然你也能这么玩:

    key = ['success', 'dangerous']
    value = ''
    
    # 返回的list
    result_list = []
    index = 0
    while index < 4:
        # 中间字典,存储数据,以及防止append覆盖
        result_dict = {}
        _key = key[0]
        value = str(index)
        result_dict[_key] = value
        index = index + 1
        result_list.append(result_dict)
    index = 0
    return_list = []
    print result_list
    while index < 4:
        # 中间字典,存储数据,以及防止append覆盖
        result_dict = {}
        _key = key[1]
        value = str(index)
        result_dict[_key] = value
        if int(result_list[index]['success']) % 2 != 0:
            dictMerge = dict(result_list[index].items() + result_dict.items())
            result_list.remove(result_list[index])
            result_list.append(dictMerge)
        index = index + 1
    print result_list

  • 相关阅读:
    比较好的总结runtime
    开发证书详解
    删除数组中特定元素推荐做法
    pbxproj文件冲突解决办法
    svn不提交user文件
    c提高第六次课 文件读取
    c++函数指针
    c提高第五次作业
    c提高第四次作业
    c提高第四课
  • 原文地址:https://www.cnblogs.com/zhiyong-ITNote/p/7381938.html
Copyright © 2011-2022 走看看