检查python字典是否包含值,如果是,则返回相关值 [英] Check if python dictionary contains value and if so return related value
本文介绍了检查python字典是否包含值,如果是,则返回相关值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,其中包含以下格式的文件名和修改日期:
fileList = [{ fileName:file1.txt,fileMod:0000048723},
{fileName:file2.txt,fileMod:0000098573}]
我需要查询字典中是否存在 fileName
,如果是,则返回该条目的fileMod值。
解决方案
使用列表解析:
<$如果item ['fileName'] == filename]
$ p $,则fileMod = [item ['fileMod'] p>
I have a list of dicts, which contain filenames and modification dates in the following format:
fileList = [{"fileName": "file1.txt", "fileMod": "0000048723"},
{"fileName": "file2.txt", "fileMod": "0000098573"}]
I need to query if a fileName
exists in the dictionary and if so return the fileMod value for that entry.
解决方案
Using a list comprehension:
fileMod = [item['fileMod'] for item in fileList if item['fileName'] == filename]
这篇关于检查python字典是否包含值,如果是,则返回相关值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文