您可以使用for循環從數組中獲取特定元素。
假設您有一組水牛buffalo,“健康healthy”動物的值為“是Yes”,而“不健康”水牛buffalo的值為“否No”。每只水牛buffalo都有一個唯一的名稱,您想隔離所有患病的水牛buffalo。
您可以使用for循環輸出所有不健康的水牛buffalo。要查看在這種情況下for循環的有用性,下面的代碼輸出數組中所有不健康的水牛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"])
以上內容為大家介紹了對數組使用PythonFor循環,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。http://www.dietsnews.net/