www.3112.net > python中如何取嵌套字典中的多个值?

python中如何取嵌套字典中的多个值?

d={'a': 2, 'b': 3, 'd': 4} dlist=list(d.keys()) 用字典的keys方法获得所有键的名字,python3需要转换为list,python2直接为list

l=[{'status':1,'com':'a'},{'status':2 ,'com':'c' },{'status':1 ,'com':'b' },{'status':1 ,'com':'a' }]l.sort(key=lambda x:(-x['status'],x['com'])) #print l

for key in e: for value in e[key]: print value Python(英语发音:/?pa?θ?n/), 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个

for key in e: for value in e[key]: print value

>>> for k, v in index.items(): print k for first, second in v: print '\t', first, second在意 6 40 9 5每次 20 99 30 11 31 69 31 80合作 0 5>>>

那就嵌套操作呗 先取键2的值,是一个字典;再对该字典做pop操作.a = {1:{1:'a',2:'b',3:'c'}, 2:{4:'d',5:'e',6:'f'}} a[2].pop(4) print a[2] a[2][5] = 'W' print a[2]

d1 = {'ser':'0001','name':'Tom','sex':'m','score':'76'} d2 = {'ser':'0002','name':'Jak','sex':'m','score':'87'} d3 = {'ser':'0003','name':'Alic','sex':'f','score':'86'} max_score = float('-inf') min_score = float('inf') max_student = None min_student = None for d in [d1

dictAll = {}for i in range(1,n+1):exec("dictAll.update(dict"+str(i)+")")

result=[]for 列表 in 变量: result.append(dict(列表))print result

>>> result = [dict([(k, item[k]) for k in rt1]) for item in rt2]>>> result[{'a': 1, 'b': 2}, {'a': 3, 'b': 3}, {'a': 2, 'b': 1}, {'a': 5, 'b': 0}]>>>>>> result = [(k, [x[k] for x in rt2]) for k in rt1]>>> result[('a', [1, 3, 2, 5]), ('b', [2, 3, 1, 0])]>>> result = [(k, sum([x[k] for x in rt2])) for k

网站地图

All rights reserved Powered by www.3112.net

copyright ©right 2010-2021。
www.3112.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com