mdsk.net
当前位置:首页 >> 如何把python list里的元素变为字典的kEy和vAluE,... >>

如何把python list里的元素变为字典的kEy和vAluE,...

tracB={} for item in B: traceB{item[0]:item[1]}

a = {1:'a',3:'b',5:'c'} b,c = a.keys() , a.values()

a = "{'a' : 'hi', 'b' : 'hello'}"b = eval(a)b{'a' : 'hi', 'b' : 'hello'} 这样转换,即把每一行获取到作为一个字符串,eval即可

字典用values()函数转化成值的列表,用items转换成(key,value)的元组列表。 列表转换成字典,需要用2个列表转化成字典,一个是key,一个是value。比如: >>>dict(zip(['a','b','c'], range(5))) {'a': 0, 'c': 2, 'b': 1} >>> a=[1,2,3] >>> ...

字典是一种hash表,即有key,和key键对应下的value 比如说我要初始化一个字典 phonebook={'Alice':'3241','Beth':'9274','Ceil':'3258'} 这样这本字典就有三对项,分别有键(key)和对应的值(value)组成 比如这里Alice,Beth,Ceil都是key 对应的valu...

习惯用zip >>> l1=[1,2,3] >>> l2=['a','b','c'] >>> dict(zip(l1,l2)) {1: 'a', 2: 'b', 3: 'c'} ======================= 楼上的map(None,)在python3下已经失效了 =============== py3的map版 >>> dict(map(lambda x,y:[x,y], l1,l2)) {1: 'a'...

result = dict() for data in number: result[data[0]] = int(result.get(data[0], 0)) + int(data[3]) print(result)

mobile=[['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']]mobiledict={}for elem in mobile: key=(elem[0],elem[1]) if key in mobiledict: mobiledict[key][0]+=int(elem...

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...

data = {1: 10002, 2: 10002, 3: 10002, 4: 10002, 5: 10002, 6: 10007, 7: 10007, 8: 10007, 9: 10007, 10: 10007, 11: 10007, 12: 10007, 13: 10007}items = list(data.items())items.sort(key=lambda i: -i[0])tmp = {val: key for key, val ...

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