简体   繁体   English

如何在python字典中检查键和值的条件以获取特定值

[英]How to check condition for key and value in python dictionary for specific value

how to check whether my key has value of <class 'inspect._empty'> 如何检查我的钥匙是否具有<class 'inspect._empty'>

Dictionary looks like : 字典看起来像:

{'abc': <class 'inspect._empty'>, 'xyz': None}

Now, I want to set abc equal to x value if it has value <class 'inspect._empty'> 现在,我要设置abc等于x值(如果它具有值) <class 'inspect._empty'>

    def _arg_unset(args_dict: Dict, key: str) -> bool:
    """return True if key is not set to some actual value in args_dict"""
    return args_dict.get(key) in {None, Parameter.empty}

In above cod, dict needs to be passed as first param and key as second param. 在上面的代码中,dict需要作为第一参数传递,而key作为第二参数传递。

>>> import inspect
>>> d = {'abc': inspect._empty, 'xyz': None}
>>> d
{'abc': <class 'inspect._empty'>, 'xyz': None}
>>> {k:v if v != inspect._empty else 'x' for k,v in d.items()}
{'abc': 'x', 'xyz': None}
>>> 

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

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