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

  • 相关阅读:
    1230 jquery
    1221 监听事件
    1218 dom表格元素操作
    1216 DOM
    Java中对小数的向下取整,向上取整
    Mysql中 在SQL语句里进行日期格式转换
    一些常用格式化。价格、日期等 持续更新
    List对象里面根据某一字段去重
    java 后端 初始化图片像素(1980 x 1080)大小
    swagger里面测试List数据格式
  • 原文地址:https://www.cnblogs.com/jonm/p/8260846.html
Copyright © 2011-2022 走看看