简体   繁体   English

Python检查字符串是否包含字典键

[英]Python check if string contains dictionary key

I have these strings: 我有这些字符串:

>>> a="foo"
>>> b="foo_KEY"

And a dictionary like: 还有一个字典,如:

>>> DICT
{
'KEY': ['...'],
'KEY2': ['...', '...'],
...
}

I'd like to build a function that check if the test value is in any key of the input dict: 我想建立一个函数来检查测试值是否在输入字典的任何键中:

>>> has_key(a, DICT)
False
>>> has_key(b, DICT)
True

What is the most elegant way to do this task in Python 3? 在Python 3中执行此任务的最优雅的方法是什么?

has_key = lambda a, d: any(k in a for k in d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM