How to check if a range of numbers supplied exist in a list? - Python

use*_*441 1 python

I have a rather cumbersome line of code:

s=[1,2,3,4,5,6,7,8,9]

if 1 in s or 2 in s or 3 in s or 4 in s or 5 in s or 6 in s or 7 in s or 8 in s or 9 in s:
    print("Yes, a number between 1-9 exists in list s)
Run Code Online (Sandbox Code Playgroud)

Is there a better way than this? I want to check if any number exists between [1-9] within list s.

Sim*_*ser 5

您可以使用any()range(1, 10)检查数字是否在s

if any(x in s for x in range(1, 10)):
    print('yes')
Run Code Online (Sandbox Code Playgroud)

这将利用生成器表达式来避免在将值传递给之前创建值的完整列表any()