I have two lists and I want to check if my list of strings contains all the elements of the second list.
I would like it to return True only if all the elements in list2 are in substrings of elements in list1 (True in this example).
Here is my code :
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
if all(elem in elem in list1.tolist() for elem in list2):
CodePudding user response:
substring = [i for e in list2 for i in list1 if e in i]
if len(susbstring) != 0:
return True
else:
return False
CodePudding user response:
Try this:
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
def check(strings, substrings):
for substring in substrings:
if not (any(substring in string for string in strings)):
return False
return True
print(check(list1, list2))
CodePudding user response:
You were close. You need to combine all and any according to this: check that all strings in list2 appear in any string in list1. Directly translates to:
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
if all(any(sub in string for string in list1) for sub in list2):
print("Success!")
