zoukankan      html  css  js  c++  java
  • python3----字典

     1 dict_test = {"Key1": "Value1", "Key2": "Value2", "Key3": "Value3"}
     2 print(dict_test.items())  # 转换成列表
     3 
     4 for k, v in dict_test.items():
     5     print("%s=%s" % (k, v))
     6 
     7 results:
     8 
     9 dict_items([('Key1', 'Value1'), ('Key2', 'Value2'), ('Key3', 'Value3')])
    10 Key1=Value1
    11 Key2=Value2
    12 Key3=Value3

    python 字典dict类型合并(不能错过哦)

     

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

     1 key = ['success', 'dangerous']
     2 value = ''
     3 
     4 # 返回的list
     5 result_list = []
     6 index = 0
     7 while index < 4:
     8     # 中间字典,存储数据,以及防止append覆盖
     9     result_dict = {}
    10     _key = key[0]
    11     value = str(index)
    12     result_dict[_key] = value
    13     index = index + 1
    14     result_list.append(result_dict)
    15 index = 0
    16 return_list = []
    17 print result_list
    18 while index < 4:
    19     # 中间字典,存储数据,以及防止append覆盖
    20     result_dict = {}
    21     _key = key[1]
    22     value = str(index)
    23     result_dict[_key] = value
    24     dictMerge = dict(result_list[index].items() + result_dict.items())
    25     return_list.append(dictMerge)
    26     index = index + 1
    27 print return_list

    程序输出:

    当然你也能这么玩:

     1 key = ['success', 'dangerous']
     2 value = ''
     3 
     4 # 返回的list
     5 result_list = []
     6 index = 0
     7 while index < 4:
     8     # 中间字典,存储数据,以及防止append覆盖
     9     result_dict = {}
    10     _key = key[0]
    11     value = str(index)
    12     result_dict[_key] = value
    13     index = index + 1
    14     result_list.append(result_dict)
    15 index = 0
    16 return_list = []
    17 print result_list
    18 while index < 4:
    19     # 中间字典,存储数据,以及防止append覆盖
    20     result_dict = {}
    21     _key = key[1]
    22     value = str(index)
    23     result_dict[_key] = value
    24     if int(result_list[index]['success']) % 2 != 0:
    25         dictMerge = dict(result_list[index].items() + result_dict.items())
    26         result_list.remove(result_list[index])
    27         result_list.append(dictMerge)
    28     index = index + 1
    29 print result_list

  • 相关阅读:
    light oj 1105 规律
    light oj 1071 dp(吃金币升级版)
    light oj 1084 线性dp
    light oj 1079 01背包
    light oj 1068 数位dp
    light oj 1219 树上贪心
    light oj 1057 状压dp TSP
    light oj 1037 状压dp
    矩阵快速幂3 k*n铺方格
    矩阵快速幂2 3*n铺方格
  • 原文地址:https://www.cnblogs.com/jonm/p/8260846.html
Copyright © 2011-2022 走看看