30秒学会 Python 片段 · 2023年8月23日

30秒学会 Python 片段 – List includes any values

Checks if any element in values is included in lst.

  • Check if any value in values is contained in lst using a for loop.
  • Return True if any one value is found, False otherwise.

代码实现

def includes_any(lst, values):
  for v in values:
    if v in lst:
      return True
  return False

使用样例

includes_any([1, 2, 3, 4], [2, 9]) # True
includes_any([1, 2, 3, 4], [8, 9]) # False

翻译自:https://www.30secondsofcode.org/python/s/includes-any