• 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

  • 相关阅读:
    洛谷 P2058 海港(模拟)
    LA 3708 墓地雕塑(模拟)
    Uva 11300 Spreading the Wealth(贪心)
    UVA 11729 Commando War (贪心)
    【洛谷习题】Likecloud-吃、吃、吃
    【洛谷习题】多米诺骨牌
    【洛谷习题】相似基因
    【NOI1995】石子合并
    【洛谷习题】尼克的任务
    【NOIP2004】合唱队形
  • 原文地址:https://www.cnblogs.com/jonm/p/8260846.html
Copyright © 2020-2023  润新知