美文网首页
The get() method on Python dicts

The get() method on Python dicts

作者: import_hello | 来源:发表于2018-10-15 11:55 被阅读0次
# 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!"

相关文章

网友评论

      本文标题:The get() method on Python dicts

      本文链接:https://www.haomeiwen.com/subject/arlboftx.html