您可以使用for循環(huán)從數(shù)組中獲取特定元素。
假設(shè)您有一組水牛buffalo,“健康healthy”動物的值為“是Yes”,而“不健康”水牛buffalo的值為“否No”。每只水牛buffalo都有一個唯一的名稱,您想隔離所有患病的水牛buffalo。
您可以使用for循環(huán)輸出所有不健康的水牛buffalo。要查看在這種情況下for循環(huán)的有用性,下面的代碼輸出數(shù)組中所有不健康的水牛buffalo的名稱:
array=[{"name":"buffalo1","healthy":"Yes"},
{"name":"buffalo3","healthy":"No"},
{"name":"buffalo4","healthy":"Yes"},
{"name":"buffalo5","healthy":"Yes"},
{"name":"buffalo8","healthy":"No"},
{"name":"buffalo7","healthy":"No"},
{"name":"buffalo9","healthy":"No"}
]
forbuffalosinarray:
ifbuffalos["healthy"]=="No":
print("Quarantine",buffalos["name"])
以上內(nèi)容為大家介紹了對數(shù)組使用PythonFor循環(huán),希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。http://www.jsszjs.cn/