python - 查找其键与子字符串匹配的字典项

标签 python dictionary substring key

我有一个像这样构造的大字典:

programs['New York'] = 'some values...' 
programs['Port Authority of New York'] = 'some values...' 
programs['New York City'] = 'some values...'
...

如何返回 programs 的所有元素,其键提到“new york”(不区分大小写)?在上面的示例中,我想获取所有三个项目。

编辑:字典非常大,预计会随着时间的推移而变得更大。

最佳答案

[value for key, value in programs.items() if 'new york' in key.lower()]

关于python - 查找其键与子字符串匹配的字典项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484261/

相关文章:

c - 在 char[] 中查找带有通配符的子字符串

python - 无法在 TensorFlow 中为 GradientDescentOptimizer 收集梯度

python - 使用 Python 的三对角矩阵

python - 除了某些字段外,如何检查 2 个字典是否相等?

python - 将整数列表转换为字符串作为排序中字典的键

mysql - SQL 中重复字符串时的子字符串索引

python - 在列表/字典错误中搜索

python - 将 python 结果返回到 html

python - 在字典中查找/映射多个键并添加到列表中

substring - Swift 2.0 substringwithrange