簡體   English   中英

如何從句子中提取單詞並檢查單詞中是否沒有單詞

[英]How to extract words from a sentence and check if a word is not there in it

這是我下面的代碼。 我需要從用戶那里獲取輸入(一個句子)並檢查其中是否有從 0 到 10 的任何數字。 我知道有很多方法可以解決它,例如split()isalnum()等,但我只需要幫助將它們放在一起。 請在下面找到我的代碼:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]

ss1 = sentence1.split()
print(ss1)
if numbers in ss1:
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence”)

謝謝 :)

編輯:預期輸出應該是這樣的:

Enter any sentence (it may include numbers): I am 10 years old
There are numbers between 0 and 10 in this sentence

我會使用正則表達式:

def contains_nums(s):
    import re
    m = re.search('\d+', s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        return f'"{s}" contains no numbers'

例子:

>>> contains_nums('abc 10')
'"abc 10" contains 10'

>>> contains_nums('abc def')
'"abc def" contains no numbers'

注意。 這只是檢查第一個數字,如果需要全部使用re.findall 這也是在單詞中查找數字,如果您只想要單獨的數字,請使用帶有單詞邊界( \\b\\d+\\b )的正則表達式,最后,如果您想限制為 0-10 個數字,請使用(?<!\\d)1?\\d(?!\\d) (或\\b(?<!\\d)1?\\d(?!\\d)\\b獨立數字)

更完整的解決方案
def contains_nums(s, standalone_number=False, zero_to_ten=False):
    import re
    
    regex = r'(?<!\d)1?\d(?!\d)' if zero_to_ten else '\d+'
    
    if standalone_number:
        regex = r'\b%s\b' % regex
    
    m = re.search(regex, s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        a = "standalone " if standalone_number else ""
        b = "0-10 " if zero_to_ten else ""
        return f'"{s}" contains no {a}{b}numbers'
>>> contains_nums('abc100', standalone_number=False, zero_to_ten=False)
'"abc100" contains 100'

>>> contains_nums('abc100', standalone_number=True, zero_to_ten=False)
'"abc100" contains no standalone numbers'

>>> contains_nums('abc 100', standalone_number=True, zero_to_ten=True)
'"abc 100" contains no standalone 0-10 numbers'

為了使您的程序以最少的更改工作,您可以使用set s:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = {"1","2","3","4","5","6","7","8","9","10","0"}

ss1 = sentence1.split()
print(ss1)
if numbers.intersection(set(ss1)):
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence")

輸出(帶編號):

Enter any sentence (it may include numbers): There is 1 number
['There', 'is', '1', 'number']
There are numbers between 0 to 10 in the sentece

輸出(無編號):

Enter any sentence (it may include numbers): There are no numbers
['There', 'are', 'no', 'numbers']
There are no numbers in the sentence

但是,這僅在要檢測的數字以空格分隔時才有效。 但話又說回來,這與您的解決方案幾乎相同,只是它使用了set

試試這個解決方案:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]
verif = False
for n in numbers:
    if n in sentence1:
        verif = True
        break
if verif:
    print("There are numbers between 0 to 10 in the sentence")
else:
    print('There are no numbers in the sentence')

輸出:

Enter any sentence (it may include numbers): gdrsgrg8
There are numbers between 0 to 10 in the sentence
import re

sentence = input("Enter a sentence: ")

def check_numbers(sentence):
    if re.search(r'\d', sentence):
        return True
    else:
        return False
                 
check_numbers(sentence)

輸入:Hello World ... 輸出:False

輸入:Hello 1 World ... 輸出:True

一種方法是檢查短語中單詞集和數字集之間的交集。 如果不為空,則有數字:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = {"1","2","3","4","5","6","7","8","9","10","0"}

ss1 = set(sentence1.split())
print(ss1)
if ss1.intersection(numbers):
    print("There are numbers between 0 to 10 in the sentece")
else:
    print("There are no numbers in the sentence")

您可以使用模式\\b(?:10|\\d)\\b匹配 10 或單詞邊界\\b之間的數字 0-9 以防止部分單詞匹配。

請注意,您的問題print("There are no numbers in the sentence”)中的消息並不完全正確。如果字符串中只有 19,則有一個數字但不會匹配,因為它不在范圍內0-10。

查看匹配的正則表達式演示

import re

sentence1 = input("Enter any sentence (it may include numbers): ")

if re.search(r"\b(?:10|\d)\b", sentence1):
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers between 0 to 10 in the sentence")
sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0"]

ss1 = sentence1.split()
for x in numbers:
    if x in ss1:
        print(x, " belongs to the sentence")
    else:
        print(x, "does not belong to a sentence")

暫無
暫無

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

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