简体   繁体   English

Python - 仅从满足特定条件的字典中提取值

[英]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.

相关问题 仅打印符合某个 output 标准的值 - Only printing values that meet a certain output criteria 解析html行并仅提取符合特定条件的数字 - Parsing an html line and pulling out only numbers that meet a certain criteria Select Pandas 中的列名 dataframe 仅当行值满足 Python 中的特定条件时 - Select column names in Pandas dataframe only if row values meet a certain criteria in Python 筛选字典以包含另一个字典中满足特定条件的值 - Filtering a dictionary to contain values from another dictionary that meet certain criteria 如何从不符合条件的数组中删除某些值? - How to remove certain values from an array, that does not meet a criteria? 如何在Python中添加在某些字符符合特定条件时分配的值? - How do I add values, that are assigned when certain characters meet certain criteria, in Python? 如何使用 Python 从 dic 中提取某些值 - How to pull certain values from dic with Python 仅比较字典列表中的某些字典键值 Python - Compare only certain dictionary key values within list of dictionaries Python 如果使用Python满足某些条件,如何从文本文件中“拉平”行? - How to 'flatten' lines from text file if they meet certain criteria using Python? 在其他列满足特定条件的情况下,如何替换NaN值? - How to replace NaN values where the other columns meet a certain criteria?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM