2016-11-08 88 views
0

我的代碼需要句子,並在該句子中找到給定的單詞。需要句子分析幫助

如果單詞在句子中,它需要說,它已發現的話,說什麼樣的立場字是。

如果字是不是在它應該顯示錯誤消息的句子。

我有這樣的:

print("Please insert your sentence without punctuation") 
sentence=(input()) 
variable1='sentence' 
print("Which word would you like to find in your sentence?") 
word=input() 
variable2='word' 
if 'word'=='COUNTRY': 
    'variable3'==5 
    'variable4'==17 
if word in sentence: 
    print([word], "is in positions", [variable3], "and", [variable4]); 
else: 
    print("Your word is not in the sentence!") 
+0

你預計' '字' =='COUNTRY''是真的嗎?你的意思是說,如果word =='COUNTRY''? – doctorlove

+2

這聽起來像是一個很短的時間的重大任務.. –

+0

這段代碼是完全沒用的。你有沒有運行過這段代碼?你能告訴我們這段代碼的哪部分不起作用嗎? – pt12lol

回答

0

的Python序列提供index方法。它給你一個元素的索引,或者如果元素不在序列中,則會產生一個錯誤。在字符串上,它允許您查找子字符串。

>>> 'hello world'.index('world') 
6 
>>> 'hello world'.index('word') 
ValueError: substring not found 

基本上,你必須添加輸入的句子和單詞來搜索。而已。

print("Insert sentence without punctuation...") 
sentence=input() # get input, store it to name `sentence` 
print("Insert word to find...") 
word=input() 
try: 
    idx = sentence.index(word) 
except ValueError: # so it wasn't in the sentence after all... 
    print('Word', word, 'not in sentence', repr(sentence)) 
else: # if we get here, IndexError was NOT thrown 
    print('Word', word, 'first occurs at position', idx) 

這裏有一些注意事項,例如'fooworldbar'也會匹配。這些事情的正確處理取決於人們想要的東西。我猜你實際上想要單詞職位。


如果您需要在「的n個字」的含義位置,你必須改變句子單詞的列表。 str.split這樣做。然後您可以再次使用index。另外,如果您想全部職位,您必須反覆調用索引。

print("Insert sentence without punctuation...") 
sentence = input() # get input, store it to name `sentence` 
words = sentence.split() # split at whitespace, creating a list of words 
print("Insert word to find...") 
word=input() 
positions, idx = [], -1 
while idx < len(words): 
    try: 
     idx = words.index(word, idx+1) 
    except ValueError: # so it wasn't in the rest of the sentence after all... 
     break 
    else: # if we get here, IndexError was NOT thrown 
     positions.append(idx) # store the index to list of positions 
# if we are here, we have searched through the whole string 
if positions: # positions is not an empty list, so we have found some 
    print('Word', word, 'appears at positions', ', '.join(str(pos) for pos in positions)) 
else: 
    print('Word', word, 'is not in the sentence') 
0

我想處理所提供的代碼中的一些誤解。

首先,

print("Please insert your sentence without punctuation") 
sentence=(input()) 

是簡單,因爲

sentence = input("Please insert your sentence without punctuation") 

現在我有一個名爲sentence變量wihich不應與字符串'sentence'

同樣可以糊塗,我們可以說

word = input("Which word would you like to find in your sentence?") 

給出了另一個變量word又不與字符串'word'

假設因爲我們有爭論的緣故被糊塗,

sentence = "Has this got an elephant in?" 

,我們搜索單詞'elephant'

張貼的代碼嘗試使用in,但會發生這種情況:

>>> "elephant" in sentence 
True 
>>> "ele" in sentence 
True 
>>> "giraffe" in sentence 
False 
>>> 

關閉。但不夠近。它不是在尋找整個詞彙,因爲我們在'elephant'中發現了'ele'

如果將split這個句子轉化爲單詞,如其他答案所示,則可以再搜索整個單詞找到該位置。 (查找拆分;您可以選擇除默認' '之外的其他字符)。

words = sentence.split() 
word = 'ele' 
words.index(word) 

如果單詞不存在,你會得到一個錯誤:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ValueError: 'ele' is not in list 

我會離開的錯誤處理給你。

0

您可以使用re模塊:

import re 

sentence = input('Sentence: ') 
word = input('Word: ') 

## convert word in regular expression for search method. 
regex_word = r'(' + word + ')(?=\s|$)' 

## initialize search var. 
search = re.search(regex_word, sentence) 

if search: 
    while search: 
     match_pos = search.span() 
     sentence = sentence[:match_pos[0]] + sentence[match_pos[1]:] 

     print(word + ' is in position ' + str(match_pos[0])) 
     search = re.search(regex_word, sentence) 
else: 
    print(word + ' is not present in this sentence')