python - Having trouble using "in" function to check for containment of one array in another -
the current code extremely short. if understand "in" function correctly, shouldn't loop iterate , return true if both [1,3] in [1,4,5]? right getting true of tests. feel there easy fix this, don't know.
i tried putting if statement in-between , return lines still returned true.
def innerouter(arr1, arr2): arr1 in arr2: return true return false
you have use if one_element in array
def innerouter(arr1, arr2): x in arr1: if x not in arr2: return false return true innerouter([1,3], [1,4,5]) # false innerouter([1,4], [1,4,5]) # true
or can use set()
check
def innerouter(arr1, arr2): return set(arr1).issubset(set(arr2)) innerouter([1,3], [1,4,5]) # false innerouter([1,4], [1,4,5]) # true
the same:
def innerouter(arr1, arr2): return set(arr1) <= set(arr2)
Comments
Post a Comment