我有这种形式的字段列表
fields = [{'name':'count', 'label':'Count'},{'name':'type', 'label':'Type'}]
我只想提取名称并将其放在列表中。目前,我正在这样做:
names = [] for field in fields: names.append(field['name'])
还有另一种方法可以做相同的事情,而不涉及遍历列表。
我正在使用python 2.7。
谢谢你的帮助。!
您可以使用列表理解:
>>> fields = [{'name':'count', 'label':'Count'},{'name':'type', 'label':'Type'}] >>> [f['name'] for f in fields] ['count', 'type']