[英]Getting Value Passing a Key From a List of Lists
I am given a list of lists, similar to the following.我得到了一个列表,类似于以下内容。 I am very new to Python我对 Python 很陌生
[
{'id': 1244},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'},
]
[
{'id': 1245},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'},
]
[
{'id': 1246},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'},
]
How can I get the "name" value from this, without having to nest loops?如何从中获取“名称”值,而不必嵌套循环?
My current code is:我目前的代码是:
for _row in _rs:
print(_row)
print(_row["name])
However, I am receiving an error message: TypeError: list indices must be integers, not str
但是,我收到一条错误消息:类型错误TypeError: list indices must be integers, not str
So, how can I accomplish this?那么,我怎样才能做到这一点?
if it trully a list of list there is a comma between each list.如果它真的是一个列表列表,则每个列表之间有一个逗号。 then you can easily do that:那么你可以轻松地做到这一点:
for i in x:
print(i[1]['name'])
How does this look?这看起来如何?
l = [{'id': 1244}, {'name': 'example.com'}, ...]
names = e['name'] for e in l if 'name' in e]
print(names)
>>> ['example.com']
You can move around the list and check if each dictionary has the "name" key, and print the result if yes.您可以在列表中移动并检查每个字典是否具有“名称”键,如果是,则打印结果。
list1 = [{'id': 1244},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'}]
for dictionary in list1:
if "name" in dictionary.keys(): # Whether the dictionary looks like {"name": ...}
print(dictionary["name"])
break # Exit the loop now that we have the name, instead of going through the whole list.
Edit: your input is broken.编辑:您的输入已损坏。 Before being able to work on it, you want to change the function that gives you what you showed in the OP into that:在能够处理它之前,您希望将提供您在 OP 中显示的内容的函数更改为:
[ # <-- outer list starts
[
{'id': 1244},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain- overview.htm'},
{'monitoring_text_string': 'quality product'},
], # <--
[
{'id': 1245},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'},
], # <--
[
{'id': 1246},
{'name': 'example.com'},
{'monitoring_enabled': 'Yes'},
{'monitoring_url': 'http://www.example.com/'},
{'monitoring_page_url': 'http://www.example.com/products-spool-chain-overview.htm'},
{'monitoring_text_string': 'quality product'},
]
] # <-- outer list ends
Even better:更好的是:
lists_of_dicts = [
[{'id': 1244,
'name': 'example.com',
'monitoring_enabled': 'Yes',
'monitoring_url': 'http://www.example.com/',
'monitoring_page_url': 'http://www.example.com/products-spool-chain- overview.htm',
'monitoring_text_string': 'quality product'}],
[{'id': 1245,
'name': 'example.com',
'monitoring_enabled': 'Yes',
'monitoring_url': 'http://www.example.com/',
'monitoring_page_url': 'http://www.example.com/products-spool-chain- overview.htm',
'monitoring_text_string': 'quality product'}],
[{'id': 1246,
'name': 'example.com',
'monitoring_enabled': 'Yes',
'monitoring_url': 'http://www.example.com/',
'monitoring_page_url': 'http://www.example.com/products-spool-chain- overview.htm',
'monitoring_text_string': 'quality product'}]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.