Python - 检查字符串中是否只有多个列表中的一个元素

时间:2016-01-08 15:33:50

标签: python string list

以下代码允许我检查ttext中是否只有一个列表元素。

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

现在,如果我在列表的元素中有空格,我怎么能这样做?"我有","你是"和"他是"?

5 个答案:

答案 0 :(得分:0)

你可以添加一个列表理解,并将所有单词分开:

def test(l, tt):
    counts = {word.strip(punctuation):0 for word in tt.split()}
    splitl = [[word for item in sublist for word in item.split(' ')] for sublist in l]
    for word in chain(*product(*splitl)):
        if word in counts:
            counts[word] += 1
        if sum(v > 1 for v in counts.values())  > 1:
            return False
    return True

答案 1 :(得分:0)

通过使用“+”连接列表而不是列表列表,可以简化很多操作。如果字符串中包含空格,则此代码也会生成单词。

import string

list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']

l = list1 + list2 + list3

def test(l, tt):
    count = 0
    for word in l:
        #set of all punctuation to exclude
        exclude = set(string.punctuation)
        #remove punctuation from word
        word = ''.join(ch for ch in word if ch not in exclude)
        if word in tt:
            count += 1
    if count > 1:
        return False
    else:
        return True

答案 2 :(得分:0)

您可以考虑使用sets进行此类处理。

这是一个快速实施:

from itertools import chain
from string import punctuation

list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']
l = list(chain(list1, list2, list3))

words = set(w.strip(punctuation) for word in l for w in word.split())  # 1

def test(words, text):
    text_words = set(word.strip(punctuation) for word in text.split())  # 2
    return len(words & text_words) == 1  # 3

很少有评论:

  1. 意图的双重循环工作,你会得到一个单词列表。该集合确保每个单词都是唯一的。
  2. 输入句子上的内容相同
  3. 使用set intersection获取句子中同样位于搜索集中的所有单词。然后使用此集的长度来查看是否只有一个。

答案 3 :(得分:0)

您可以通过迭代来分割所有列表输入。类似的东西:

words=[]

for list in l:
    for word in list:
        string=word.split()
        words.append(string)

答案 4 :(得分:0)

好吧,首先,让我们重写函数更自然:

from itertools import chain

def only_one_of(lists, sentence):
  found = None
  for item in chain(*lists):
    if item in sentence:
      if found: return False
      else: found = item
  return True if found not is None else False

这已经适用于你的约束,因为它只是寻找一些字符串item作为sentence的子字符串。它是否包含空格并不重要。但它可能会导致意想不到的结果。想象:

list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']

l = [list1, list2, list3]

only_one_of(l, 'Cadabra')

这会返回True,因为abraCadabra的子字符串。如果这是你想要的,那么你就完成了。但如果没有,你需要重新定义item in sentence的真正含义。所以,让我们重新定义我们的功能:

def only_one_of(lists, sentence, is_in=lambda i, c: i in c):
  found = None
  for item in chain(*lists):
    if is_in(item, sentence):
      if found: return False
      else: found = item
  return True if found not is None else False

现在,最后一个参数应该是一个函数,应用于两个返回True的字符串,如果在第二个字符串中找到第一个字符串,或者在其他地方找到False

您通常想要检查项目是否在句子中作为单词(但是可以在中间包含空格的单词),所以让我们使用正则表达式来做到这一点:

import re
def inside(string, sentence):
  return re.search(r'\b%s\b' % string, sentence)

True位于string时,此函数会返回sentence,但会将string视为一个单词(正则表达式中的特殊序列\b代表字边界)。

因此,以下代码应该通过您的约束:

import re
from itertools import chain

def inside(string, sentence):
  return re.search(r'\b%s\b' % string, sentence)

def only_one_of(lists, sentence, is_in=lambda i, c: i in c):
  found = None
  for item in chain(*lists):
    if is_in(item, sentence):
      if found: return False
      else: found = item
  return True if found not is None else False

list1 = ['abra', 'hello', 'cfre']
list2 = ['dacc', 'ex', 'you', 'fboaf']
list3 = ['ihhio', 'oih', 'oihoihoo']
list4 = ['I have', 'you are', 'he is']

l = [list1, list2, list3, list4]

only_one_of(l, 'hello my name is brian', inside) # True
only_one_of(l, 'hello how are you?', inside) # False
only_one_of(l, 'Cadabra', inside) # False
only_one_of(l, 'I have a sister', inside) # True
only_one_of(l, 'he is my ex-boyfriend', inside) # False, ex and boyfriend are two words
only_one_of(l, 'he is my exboyfriend', inside) # True, exboyfriend is only one word