[英]Python - Pull Out Values from Only Dictionaries that Meet a Certain Criteria
I have a Python list called "results" that has dictionaries as its values:我有一个名为“结果”的 Python 列表,其中包含字典作为其值:
results = [
{
'postingStatus': 'Active',
'postEndDate': '1601683199000',
'boardId': '_internal',
'postStartDate': '1591084714000)'
},
{
'postingStatus': 'Expired',
'postEndDate': '1601683199000',
'boardId': '_external',
'postStartDate': '1591084719000)'
}
]
How would I create a list that gets all the values from the dictionary where the 'boardID' value is '_internal' (but ignores the dictionary where 'boardID' is '_external')?我将如何创建一个从字典中获取所有值的列表,其中“boardID”值为“_internal”(但忽略“boardID”为“_external”的字典)? As an end result, I'm hoping for a list with the following contents:
作为最终结果,我希望得到一个包含以下内容的列表:
['Active','1601683199000','_internal','1591084714000']
You can use list-comprehension:您可以使用列表理解:
out = [v for d in results for v in d.values() if d["boardId"] == "_internal"]
print(out)
Prints:印刷:
['Active', '1601683199000', '_internal', '1591084714000)']
without using List-comprehension不使用列表理解
for items in results:
if items['boardId'] == '_internal':
lst1 = list(items.values())
print(lst1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.