I have 2 lists:
JavaScript
x
3
1
a = ["ad", "news", "something else", "another something"]
2
c = ["coupon", "ad"]
3
List a (and c ) must contain only characters from list b. Also in the list a(and c ) can be missed characters from list b but the characters from the list be must appear or partially :
JavaScript
1
2
1
b = ["coupon", "ad", "news"]
2
As a result, list a (because it contains additional characters) is wrong and list c is OK (although- it doesn’t have “news”).
I started writing nested if and I am stuck
JavaScript
1
4
1
for x in ["coupon", "ad", "news"]:
2
for z in ["ad", "news", "something else", "another something"]:
3
print(x,z)
4
Advertisement
Answer
I have defined 2 functions.
The first one will take in 2 lists of strings, and return if any of the strings in the first list exists in the second list:
JavaScript
1
11
11
1
def validate(lst1, lst2):
2
return all(i in lst2 for i in lst1)
3
4
a = ["ad", "news", "something else", "another something"]
5
c = ["coupon", "ad"]
6
7
b = ["coupon", "ad", "news"]
8
9
print(validate(a, b))
10
print(validate(c, b))
11
Output:
JavaScript
1
3
1
False
2
True
3
The second one checks if all the characters used the each string in the first list exists in any of the strings in the second list:
JavaScript
1
12
12
1
def validate(lst1, lst2):
2
characters = ''.join(lst2)
3
return all(j in characters for i in lst1 for j in i)
4
5
a = ["ad", "news", "something else", "another something"]
6
c = ["coupon", "ad"]
7
8
b = ["coupon", "ad", "news"]
9
10
print(validate(a, b))
11
print(validate(c, b))
12
Output:
JavaScript
1
3
1
False
2
True
3