简体   繁体   English

如何在python中搜索列表中可能不存在的内容?

[英]How can I search through a list in python for something that might not exist?

For example something like: 例如:

if l.index(a)== -1:
    l += [a]

If I run something like this, I will get a value error.I am assuming this is not a new problem. 如果我运行这样的东西,我将得到一个值错误。我假设这不是一个新问题。

Why not just use: 为什么不使用:

if a not in some_list:
    some_list.append(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM