gpt4 book ai didi

python - 哪种方法更好地跳过 'NoneType' 变量?

转载 作者:太空狗 更新时间:2023-10-30 00:31:45 26 4
gpt4 key购买 nike

一个列表包含多个 NoneType 元素。要跳过 NoneType

for item in list :
if item is not None :
fp.write(item + '\n')

#OR

for item in list :
try :
fp.write(item + '\n')
except :
pass

哪个更好,为什么?

最佳答案

根据一般经验,如果可以的话,您不应该真正使用 try: except: 模式来控制流。引发在此上下文中不必要的异常会产生一些开销。希望这会有所帮助。

关于python - 哪种方法更好地跳过 'NoneType' 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31587060/

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