【问题标题】:find only one word of multiple lists in a string在一个字符串中只找到多个列表中的一个单词
【发布时间】:2016-01-04 16:51:37
【问题描述】:
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' then 'ok there's only one word 'hello' of the lists that is in ttext' 但如果我有“你好,你好吗”,我有“你好”和“你”,所以“列表中的两个单词在 ttext 中不行”

【问题讨论】:

  • 您是否在询问是否至少有一个列表 (L),其中 ttext 包含不超过一个来自 L 的单词?或者您是在问是否有一个列表 (L),其中 ttext 恰好包含 L 中的一个单词?或者您是在问是否最多有一个列表 (L),其中 ttext 最多包含 L 中的一个单词?有很多可能的方法可以做到这一点,从你的问题中并不清楚你想要哪一种。
  • @ChadS.exactly 一个列表(L),其中 ttext 恰好包含 L 中的一个单词一个词我需要一个结果,如果在所有列表中只有一个元素在 ttext == ok (不是每个列表一个元素,对于所有检查的列表,我需要知道是否只有一个词在 ttext 中)
  • @mathieu,一些例子会有所帮助
  • @PadraicCunningham '你好,我的名字是 mathieu' 没问题,因为里面只有“你好”这个词(列表中有 1 个词)但是“你好,你好吗?”不行,因为列表中有两个词“你好”和“你”。
  • @mathieu,你想要一个字还是 1 个字或没有字?

标签: python string enumerate


【解决方案1】:

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

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

【讨论】:

  • 我得到了这个pastebin.com/Y7zfdrMA 现在我怎么能知道列表中有两个词(所以不行)“你好”和“你”在 ttext 中所以“不行”
  • @mathieu,我改了代码,输出有什么不正确的地方?
  • 好的,我得到了相同的结果,但我不知道如何使用它,我想知道一个输出是否“列表中只有一个单词在 ttext 中”然后 var =是的
  • 在函数中生成它
  • 很抱歉,但这样我仍然有多个 True 和 False,我想知道在 ttext 中是否只有一个单词在列表中,所以我可以做点什么。 '如果 ttext 中只有一个单词在列表中“然后 a = True 否则 a = False。然后在我使用 'if a = true: do somthing' 之后
【解决方案2】:

不确定你想在这里实现什么,但你可以尝试这样的事情:

for words in product(*l):
    print(words, len(word for word in words if word in ttext) == 1)

【讨论】:

    【解决方案3】:
    for list in l:
        if 'hello' in list:
            print(list, 'this one got hello in it')
    

    或者也许不使用 all 使用 any print(words, any(word in ttext for word in words))

    【讨论】:

    • 但是如果不仅有 'hello','any' 将匹配任何单词。我想知道列表中是否只有一个单词在 ttext 中(不一定是 hello)跨度>
    【解决方案4】:

    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 的计算结果为 true 或 false。

    【讨论】:

    • 请给你的答案添加一些解释!
    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多