簡體   English   中英

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

[英]Python - Check if there's only one element of multiple lists in a string

以下代碼使我可以檢查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

現在,如果我在列表的元素“我有”,“你是”和“他是”中有空格,我該怎么做?

您可以添加一個列表理解,它可以遍歷並拆分所有單詞:

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

您可以通過使用“ +”串聯列表而不是使用列表列表來簡化很多操作。 如果字符串中包含空格,則此代碼也將顯示單詞。

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

您可以考慮將集合用於這種處理。

這是一個快速實現:

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. 意圖上的雙重for循環有效,您可以獲得單詞列表。 該集合確保每個單詞都是唯一的。
  2. 在輸入句子上有同樣的事情
  3. 使用集合交集獲取句子中所有也在搜索集中的單詞。 然后使用該集合的長度來查看是否只有一個。

您可以通過迭代遍歷所有列表輸入。 就像是:

words=[]

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

好吧,首先,讓我們重寫函數使其更加自然:

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)

stringsentence但將string視為單詞時,此函數返回True (正則表達式中的特殊序列\\b表示單詞boundary )。

因此,以下代碼應克服您的約束:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM