首页 > 其他 > 详细

sh_21_遍历字典的列表

时间:2019-09-14 16:38:56      阅读:430      评论:0      收藏:0      [点我收藏+]

 

sh_21_遍历字典的列表

students = [
    {"name": "阿土"},
    {"name": "小美"}
]

# 在学员列表中搜索指定的姓名
find_name = "张三"

for stu_dict in students:

    print(stu_dict)

    if stu_dict["name"] == find_name:

        print("找到了 %s" % find_name)

        # 如果已经找到,应该直接退出循环,而不再遍历后续的元素
        break
    # else:
    #     print("抱歉没有找到 %s" % find_name)
else:
    # 如果希望在搜索列表时,所有的字典检查之后,都没有发现需要搜索的目标
    # 还希望得到一个统一的提示!
    print("抱歉没有找到 %s" % find_name)

print("循环结束")

 

sh_21_遍历字典的列表

原文:https://www.cnblogs.com/shaohan/p/11519390.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!