30秒学会 Python 片段 · 2023年7月12日

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

Checks if all the elements in values are included in lst.

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

代码实现

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

使用样例

includes_all([1, 2, 3, 4], [1, 4]) # True
includes_all([1, 2, 3, 4], [1, 5]) # False

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