gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:09 24 4
gpt4 key购买 nike

<分区>

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

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com