python - 如何检查字典是否有某个键(Python)

标签 python dictionary key

我有一个 defaultdict 字典,它有这样的键:

RJECNIK['*','A']['<A>']

现在我不知道如何检查是否有 key ,例如:

a=list(RJECNIK.keys())

仅提供第一个键 (['*','A']) 的列表。 在我的代码中,我需要一个 if 语句

如果在 RJECNIK 中键入: ...

但它不起作用,因为我不知道如何使用 2 个键检查 defaultdict 中的一对键。

最佳答案

您需要检查两个字典中的两个键:

key = ('*', '<A>')
if key[0] in RJECNIK and key[1] in RJECNIK[key[0]]:
    pass

关于python - 如何检查字典是否有某个键(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19792157/

相关文章:

python - 无法导入名称(模型)

python - 如何从航类预订网站 https ://reservations. airarabia.com 获取价格信息

python - 循环遍历字典列表python

python - 引用的字典键是绝对必须的吗?

Java TreeMap 获取方法

python - 如何在 Python 文档字符串测试中处理四舍五入到负零

python - 在python中的不规则网格上集成二维数据

python - python 中最好的有序 dict 实现是什么?

python - 提高列表删除的时间效率

java - 使用监视文件夹重新启动线程