转换列表列表 [英] convert list of dicts to list
本文介绍了转换列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个表单中的字段列表
I have a list of fields in this form
fields = [{'name':'count', 'label':'Count'},{'name':'type', 'label':'Type'}]
我想只提取名字并将其放在列表中。目前,我正在这样做:
I'd like to extract just the names and put it in a list. Currently, I'm doing this:
names = []
for field in fields:
names.append(field['name'])
有另一种方法来做同样的事情吗不涉及循环列表。
Is there another way to do the same thing, that doesn't involve looping through the list.
我使用的是python 2.7。
I'm using python 2.7.
感谢您的帮助
推荐答案
您可以使用列表解析:
>>> fields = [{'name':'count', 'label':'Count'},{'name':'type', 'label':'Type'}]
>>> [f['name'] for f in fields]
['count', 'type']
这篇关于转换列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文