本文摘自php中文网,作者巴扎黑,侵删。
这篇文章主要为大家详细介绍了python字典DICT类型合并,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文为大家分享了python字典DICT类型合并的方法,供大家参考,具体内容如下
我要的字典的键值有些是数据库中表的字段名, 但是有些却不是, 我需要把它们整合到一起, 因此有些这篇文章.(非得凑够150个字,我也是没有办法,扯一点昨天的问题吧,话说python中的session就只能在requests库中发挥作用?就不能想asp.net中那样存值,然后设置过期时间以便验证?我原本是想在python中找个与asp.net中的cache差不多功能的库,结果,缓存那块python好像就是redis和memcached,mongodb之类的,有倒是有一两个,但是在项目里用不上了,web.py中用webpy自己的session也有问题,不能跨.py调用嘛?后续研究吧)
程序示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | key = [ 'success' , 'dangerous' ]
value = ''
result_list = []
index = 0
while index < 4 :
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 :
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
|
程序输出:

当然你也能这么玩:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | key = [ 'success' , 'dangerous' ]
value = ''
result_list = []
index = 0
while index < 4 :
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 :
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
|

以上就是python字典DICT类型合并详解的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python怎么定义线程局部变量
mac 正确地配置 scipy 开发环境
如何打开Python3
学Python能做什么的
怎么在电脑上下载Python
Python怎么输出列表
Python安装的包如何寻找
Python统计单词出现次数
详细讲解 Python中的正则表达式
如何用Python实现微信消息防撤回
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python字典DICT类型合并详解