Python 从列表中提取所需元素,并制作另一个列表
我有下面的列表,我只想从中提取姓名,然后再列出另一个列表。有人能帮我做到这一点吗Python 从列表中提取所需元素,并制作另一个列表,python,python-2.7,Python,Python 2.7,我有下面的列表,我只想从中提取姓名,然后再列出另一个列表。有人能帮我做到这一点吗 >>> a = [{'name': 'mytestbuild', 'notes': 'Autocreated build.', 'testplan_id': '178775', 'closed_on_date': '', 'release_date': '', 'is_open': '1', 'active': '1', 'creation_ts': '2017-05-09 11:20:25',
>>> a = [{'name': 'mytestbuild', 'notes': 'Autocreated build.', 'testplan_id': '178775', 'closed_on_date': '', 'release_date': '', 'is_open': '1', 'active': '1', 'creation_ts': '2017-05-09 11:20:25', 'id': '160'}, {'name': 'mytestbuild_delete', 'notes': 'Autocreated build.', 'testplan_id': '178775', 'closed_on_date': '', 'release_date': '', 'is_open': '1', 'active': '1', 'creation_ts': '2017-05-18 15:24:37', 'id': '164'}]
从中,我只想得到“名称”列表,即
name_list = ['mytestbuild', 'mytestbuild_delete']
这不起作用:
>>> a['name']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>a['name']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:列表索引必须是整数,而不是str
你应该这样做
print (a[0]['name'])
这会打印出来
mytestbuild
并将其列为一个列表
lis= [m['name'] for m in a]
print (lis)
这将打印列表
['mytestbuild', 'mytestbuild_delete']
name\u list=[item['name']用于a中的项目]
能否请您将其作为答案发布,我将接受它。非常感谢你!