Python 如何检查字典是否包含多个元素?

Python 如何检查字典是否包含多个元素?,python,dictionary,Python,Dictionary,我有一个二维数组,每个单元格都包含一个字典,其中随机填充了一个人、蚊子或两者。这看起来像这样: {'human': Human instance, 'mosquitoes': [Mosquito instance]} 我在二维数组中循环,并检查每个单元格: for row in my_array: for cell in row: if cell['human']: do this elif cell['mosquitoes']:

我有一个二维数组,每个单元格都包含一个字典,其中随机填充了一个人、蚊子或两者。这看起来像这样:

{'human': Human instance, 'mosquitoes': [Mosquito instance]}
我在二维数组中循环,并检查每个单元格:

for row in my_array:
    for cell in row:
        if cell['human']:
            do this
        elif cell['mosquitoes']:
            do this
        elif cell[both]:
            do this
我已经试过了建议的方法,但到目前为止还没能成功
单元格[两者]
将永远不会运行,因为它是最后一次
elif
检查。如果可能的话,第一个

if cell['human'] and cell['mosquitoes']:
    do this
elif cell['human']:
    do this
elif cell['mosquitoes]:
    do this

请注意,如果
人类
蚊子
键不存在,您可能会得到
KeyError
。因此,您可能需要使用
cell.get(key)
语法,而不是
cell[key]
,以适应此类事件。

字典中“两者”的示例是什么?您会遇到什么错误?您可能希望以某种方式“跟踪”您遇到的错误数量。如何存储包含多次出现的值?不确定我是否理解该问题,是否需要一种同时测试这两个值的方法(如果字典中有两个键)?如果是这样的话,
elif cell['human']和cell['munds']:
应该可以做到这一点。你能展示更多的代码,让我们更好地理解你的逻辑流程吗?因为它不会执行下面的代码。它不会进入elif。非常感谢您提供的有用答案和您使用cell.get(key)的建议。
if cell['human'] and cell['mosquitoes']:
    do this
elif cell['human']:
    do this
elif cell['mosquitoes]:
    do this