python – 如何使用逗号分隔变量?

我有一个这样的变量:ignore = val1,val2

但我不清楚如何将这些值用作单独的值.

目前(据我所知)我需要像下面的代码一样对它们进行硬编码:

if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ):
    etc

现在我想要测试添加到它,再次我需要像这样硬编码:

if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ) and (not Path.find("test") > -1 ):

这样做有没有更好的方法?

最佳答案 如果ignore是值名称的元组:

if all(Path.find(v) <= -1 for v in ignore):

这有利于在第一个条件为假时立即停止.就像您的硬编码示例一样.

点赞