python - Python 字典 - 如果 dict 值为 None,则 dict.get() 返回值
问题描述
None
如果 dict 中的值使用类似的方法,有没有办法返回另一个值get
?
IE。
my_dict = {
"my_key": None
}
我想要实现的类似于这个逻辑:
my_variable = my_dict.get("my_key", "Not None")
# After executing the line above the value of my_variable should be "Not None"
我知道get
检查字典中是否存在密钥,如果密钥不存在,则返回第二个参数。有没有类似的方法或单线来做到这一点?
解决方案
唯一的方法是在检索值后检查
my_dict = {'my_key': None}
my_value = "None Value" if my_dict.get('my_key', "") is None else my_dict.get('my_key', "")
print(my_value)
# None Value
推荐阅读
- ios - 如何检查用户电子邮件是否已验证?
- python - 当前 AWS 身份不是角色:arn:aws:iam::XXXXXXXXXXX:root,因此不能用作 SageMaker 执行角色
- java - Apache Iceberg 无法将数据写入表中
- r - 如何在 Ubuntu 18.04 上更新 R 使用的 libcurl 版本
- c# - c# Intersect 2 列表具有相同但不相同的对象
- javascript - Angular 7 中的 JWT 刷新令牌 - API 在 HTTP 拦截器中多次调用
- angular - Ag 网格不显示滚动条
- sql - 如何对重复的 SQL/PSQL 求和
- microsoft-graph-api - 请求订阅 Microsoft Graph Subscripton for Teams 时出错
- database - MS Access中的vba sql查询