我有一个名为the_dictionary_list
:的字典
the_dictionary_list= {'Color': ['Amarillo.png', 'Blanco.png',
'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos':
['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas':
['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas':
['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
我想检查它是否有一个具有特定值的特定键(在它的数组中(,所以我键入了以下代码:
if ('Color', 'None') in the_dictionary_list.items():
print('This is True')
else:
print('This is False')
if ('Color', 'Amarillo.png') in the_dictionary_list.items():
print('This is True')
else:
print('This is False')
输出如下:
这是错误的
这是错误的
这是50%的错误,因为第二个if statement
应该打印This is True
,我试图在其他网站上寻找答案,但里面有数组的字典似乎很少见,但最终是合法的,否则我如何评估上面的字典中是否真的存在一对特定的键值?
我只需使用一个函数:
the_dictionary_list= {
'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'],
'Fondo': ['Oceano.png'],
'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
def existe(llave, valor, dicc):
return llave in dicc and valor in dicc[llave]
print(existe('Color', 'None', the_dictionary_list))
print(existe('Color', 'Amarillo.png', the_dictionary_list))
输出:
错误
真实