我有以下字典稱為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,我試圖在其他網站上尋找答案,但似乎帶有陣列的字典非常不常見,但最終是合法的,我還能如何評估特定的一對鍵-value 確實存在于上面的字典中?
uj5u.com熱心網友回復:
我想通了,只需使用一個函式:
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))
輸出:
錯誤的
真的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/395691.html
