Skip to main content
 首页 » 编程设计

python之是否有访问列表的 "get or default"方式

2025年05月04日12sxdcgaq8080

<分区>

我喜欢函数 get,它可以提供默认值,但它只适用于字典。

s=dict{} 
s.get("Ann", 0) 

我为列表写了一些类似的东西。这个函数在Python3.4中已经存在了吗?

def get(s, ind): 
    return len(s)>ind and s[ind] or 0