from itertools import product
ttext = 'hello how are you?'
list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']
l = [list1, list2, list3]
So i have the lists above and i need to know if there's only one word of the lists that is in ttext (only one)
What i want: for example if in ttext i have "hello my name is brian" it's going to say 'ok there's only one word' but if i have more than one word of the lists in ttext then 'error'...
here it checked if all the words are in ttext, how can i do to check if there's only one word of all the list that is in ttext
for words in product(*l):
print(words, all(word in ttext for word in words))
(('abra', 'dacc', 'ihhio'), False)
(('abra', 'dacc', 'oih'), False)
(('abra', 'dacc', 'oihoihoo'), False)
(('abra', 'ex', 'ihhio'), False)
(('abra', 'ex', 'oih'), False)
(('abra', 'ex', 'oihoihoo'), False)
(('abra', 'you', 'ihhio'), False)
(('abra', 'you', 'oih'), False)
(('abra', 'you', 'oihoihoo'), False)
(('abra', 'fboaf', 'ihhio'), False)
(('abra', 'fboaf', 'oih'), False)
(('abra', 'fboaf', 'oihoihoo'), False)
(('hello', 'dacc', 'ihhio'), False)
(('hello', 'dacc', 'oih'), False)
(('hello', 'dacc', 'oihoihoo'), False)
(('hello', 'ex', 'ihhio'), False)
(('hello', 'ex', 'oih'), False)
(('hello', 'ex', 'oihoihoo'), False)
(('hello', 'you', 'ihhio'), False)
(('hello', 'you', 'oih'), False)
(('hello', 'you', 'oihoihoo'), False)
(('hello', 'fboaf', 'ihhio'), False)
(('hello', 'fboaf', 'oih'), False)
(('hello', 'fboaf', 'oihoihoo'), False)
(('cfre', 'dacc', 'ihhio'), False)
(('cfre', 'dacc', 'oih'), False)
(('cfre', 'dacc', 'oihoihoo'), False)
(('cfre', 'ex', 'ihhio'), False)
(('cfre', 'ex', 'oih'), False)
(('cfre', 'ex', 'oihoihoo'), False)
(('cfre', 'you', 'ihhio'), False)
(('cfre', 'you', 'oih'), False)
(('cfre', 'you', 'oihoihoo'), False)
(('cfre', 'fboaf', 'ihhio'), False)
(('cfre', 'fboaf', 'oih'), False)
(('cfre', 'fboaf', 'oihoihoo'), False)
EDIT: if only one word of the lists is in ttext: if ttext = 'hello my name is brian' then 'ok there's only one word 'hello' of the lists that is in ttext' but if i have 'hello how are you' i have 'hello' and 'you' so 'not ok two word of the lists are in ttext'
Ok I think we are finally there:
from itertools import product, chain
from string import punctuation
list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']
l = [list1, list2, list3]
def test(l, tt):
counts = {word.strip(punctuation):0 for word in tt.split()}
for word in chain(*product(*l)):
if word in counts:
counts[word] += 1
if sum(v > 1 for v in counts.values()) > 1:
return False
return True
Output:
In [16]: ttext = 'hello my name is brian'
In [17]: test(l,ttext)
Out[17]: True
In [18]: ttext = 'hello how are you?'
In [19]: test(l,ttext)
Out[19]: False