tnjk.net
当前位置:首页 >> Python将两个字典合并成一个字典 >>

Python将两个字典合并成一个字典

比如:l1=[1,2,3] l2=['baidu','google','bing'] py3.x中 dict(map(lambda x,y:[x,y], l1,l2)) {1:'baidu', 2:'google', 3:'bing'} py2.x中 dict(zip(l1,l2))

dict1={'1': ('a', 'b', 'c', 'd'), '2': ('f', 'w',你估计根本没有把这些内容用python运行过吧?看我给你改的 合并2个字典

可以使用 zip 函数: keys = [1, 2, 3]values = ['a', 'b', 'c']dic = dict(zip(keys, values))print(dic)运行结果:

list1=[1,2,3,4,5]list2=['a','b','c','d','e']dict1=dict(zip(list1,list2))print dict1

dict(zip(lista,listb)) zip函数将lista,listb的元素两两组合在一起,dict将zip的返回值强制转换为字典 生成一个以lista成员为key,listb元素为value的字典

list1 = ['1','2','3']list2 = ['a','b','c']list3 = ['A','B','C']d = {}for i in range(0, len(list1)): d[list1[i]]=(list2[i], list3[i]);print(d)

l1 = ['PID', 'USERNAME', 'THR', 'PRI', 'NICE', 'SIZE', 'RES', 'STATE', 'TIME', 'WCPU', 'COMMAND']l2 = ['32', 'root', '1', '-36', '-155', '0K', '16K', 'WAIT', '0:00', '0.00%', 'swi3:', 'ip6opt', 'ipopt']d=dict(zip(l1[:-1],l2[:-3...

Python编程将多个字典文件合并成一个字典文件,代码如下: //例子:合并a.txt和b.txt两个字典文件def readf(filename): lines = file(filename).readlines() dic = {} for i in lines: i_ = i.split() dic[i_[0]] = int(i_[1]) return dic dica ...

info= [{'name':'apple','value':2},{'name':'orange','value':5},{'name':'apple','value':5}]info_dic={}for d in info: if d['name'] not in info_dic: info_dic[d['name']]=d['value'] else: info_dic[d['name']]+=d['value']new_info=[]for...

l = [['1号机器', '01'], ['2号机器', '02']] dic = '''{"01":{"list":[{"ID":"a","Name":"Ua"},{"ID":"b","Name":"Ub"}]},"02":{"list":[{"ID":"c","Name":"Uc"},{"ID":"d","Name":"Ud"}]}}'''dic = dic.replace(',', ',')dic = eval(dic)resu...

网站首页 | 网站地图
All rights reserved Powered by www.tnjk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com