发布时间:2022-03-10 10:01:19 人气:284 作者:多测师
您可以使用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"}
]
for buffalos in array:
if buffalos["healthy"]=="No":
print("Quarantine", buffalos["name"])
以上内容为大家介绍了对数组使用Python For循环,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/