欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

马哥 今日小技巧 字典中get方法设置默认值 有大用

 字典中get方法设置默认值



# The get() method on dicts

# and its "default" argument


name_for_userid = {

    382: "Alice",

    590: "Bob",

    951: "Dilbert",

}


def greeting(userid):

    return "Hi %s!" % name_for_userid.get(userid, "there")


>>> greeting(382)

"Hi Alice!"


>>> greeting(333333)

"Hi there!"


普通分类: