有没有更快的方法来检查列表/集中的一堆东西?

时间:2019-03-07 03:42:55

标签: python list if-statement set

我必须检查数字1到9是否在列表/集合中

if 1 in factor_numbers and 2 in factor_numbers and 3 in factor_numbers and 4 in factor_numbers and 5 in factor_numbers and 6 in factor_numbers and 7 in factor_numbers and 8 in factor_numbers and 9 in factor_numbers and 0 in factor_numbers:

必须有一种更有效的方法来执行此检查?这些数字不一定彼此相邻

1 个答案:

答案 0 :(得分:2)

您可以使用为集合定义的定义方法set.issubset,例如:

set([1, 2, 3, 4, 5, 6, 7, 8, 9]).issubset(set(factor_numbers))