例如:
persons = [{'id': 1, 'name': 'john'}, {'id': 2, 'name': 'mary'}, {'id': 3, 'name': 'tom'}]
我想从中得到两个列表:
ids = [1, 2, 3]
names = ['john', 'mary', 'tom']
我做了什么:
names = [d['name'] for d in persons]
ids = [d['id'] for d in persons]
有更好的方法吗?
我会坚持使用列表理解或使用@Woodford 技术
ids,name = [dcts['id'] for dcts in persons],[dcts['name'] for dcts in persons]
output
[1, 2, 3]
['john', 'mary', 'tom']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)