快捷方式仅打印词典列表中的一项

问题描述

|
l = [
    {\'bob\':\'hello\',\'jim\':\'thanks\'},{\'bob\':\'world\',\'jim\':\'for\'},{\'bob\':\'hey\',\'jim\':\'the\'},{\'bob\':\'mundo\',\'jim\':\'help\'}
]

for dict in l:
    print dict[\'jim\']
有没有一种衬里或蟒蛇的方式来做到这一点? 我正在尝试检索词典列表中仅一项的列表     

解决方法

        
[d[\'jim\'] for d in l]
并且不要将ѭ2用作变量名。它掩盖了内置的
dict()
。     ,        是的,具有良好的功能编程:
map(lambda d: d[\'jim\'],l)
    ,        当然可以,例如:
In []: l
Out[]: 
[{\'bob\': \'hello\',\'jim\': \'thanks\'},{\'bob\': \'world\',\'jim\': \'for\'},{\'bob\': \'hey\',\'jim\': \'the\'},{\'bob\': \'mundo\',\'jim\': \'help\'},{\'bob\': \'gratzie\',\'jimmy\': \'a lot\'}]
In []: [d[\'jim\'] for d in l if \'jim\' in d]
Out[]: [\'thanks\',\'for\',\'the\',\'help\']
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...