繁体   English   中英

在字符串中仅找到多个列表中的一个单词

[英]find only one word of multiple lists in a string

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]

所以我有上面的列表,我需要知道列表中只有一个单词在ttext中(只有一个)

我想要的是:例如,如果在ttext中我有“你好,我的名字叫brian”,它会说“好,那里只有一个字”,但是如果我在ttext中有多个单词,则“错误” ...

在这里它检查所有单词是否都在ttext中,我该怎么做才能检查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)

编辑:如果列表中只有一个单词在ttext中:如果ttext ='hello my name is brian',那么'好,列表中只有一个单词'hello'在ttext中',但是如果我有'hello你好吗'我有'你好'和'你',所以'不好吧,列表中的两个词都在文本中'

好吧,我想我们终于可以到了:

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

输出:

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

不知道您要在这里实现什么,但是您可以尝试执行以下操作:

for words in product(*l):
    print(words, len(word for word in words if word in ttext) == 1)
for list in l:
    if 'hello' in list:
        print(list, 'this one got hello in it')

或者也许不使用全部使用任何print(words, any(word in ttext for word in words))

Python将两个布尔值的总和视为1和0。 这意味着您可以通过添加列表中的所有值并检查其是否为1来检查列表中的一个真实值。以下函数将检查布尔值列表中是否存在x个真实值:

def xNumberAreTrue(booleans, x):
    return sum(booleans) == x

在您的情况下,可以按以下方式使用此功能:

for words in product(*l):
    print(words, xNumberAreTrue([word in ttext for word in words], 1))

word in ttext中的word in ttext评估为truefalse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM