I have a list of words:
words = ['ABC', 'CDE', 'EFG']
How to check that my string only consists words from that list? For example, 'EFG CDE'
results True
since both 'CDE'
and 'EFG'
are in words
.
My code is below:
lmn = []
for j in list(itertools.permutations(words, 2)) + list(itertools.permutations(words, 3)):
lmn.append(' '.join(j))
'EFG CDE' in lmn
My output is giving True
which is correct.
But for strings like 'EFG EFG CDE'
, 'CDE CDE CDE CDE'
it will not give True
because these strings are not present in lmn
. Even if they are made of the list ['ABC', 'CDE', 'EFG']
only.