2016-05-24 31 views
0

我需要輸入一個混合了大寫和小寫字母的句子,並輸出每個單詞的位置。但是,如果一個單詞用大寫字母表示,並且僅輸入小寫字母的相同單詞,則它被視爲單獨的單詞。例如。如果輸入書和書,這些將被計爲兩個單獨的詞。我嘗試過使用.upper/.lower函數,但是在這樣做之後沒有辦法重新輸入大寫字母?我需要刪除大寫字母,然後再放回去。如何去掉大寫字母,然後再放回到同一個地方?

def analyse_sentence(): 
     sentence=input("Please enter your sentence") 
     sentence=sentence.split 
     compression(sentence) 

    def compression(sentence): 
     positions=[] 
     Unique_words=[] 
    for i in sentence: 
     if i not in unique: 
     unique.append(i) 
     positions.append(unique_words.index(i)+1) 
+3

製作副本,並從副本中刪除帽字母。 – Marichyasana

+0

請提供您自己的代碼示例以及究竟是什麼導致問題。正如@Marichyasana所提到的那樣,解決方案可以像使用單獨的引用一樣簡單,但是如果沒有看到自己的實現,這個問題是否可以完成還不是很清楚。 –

+0

你的輸出應該是什麼樣子?例如,它是一個字典,其中包含每個單詞的鍵和句子中位置的值? –

回答

0

當發佈堆棧溢出時,您應該提供代碼示例。 你有什麼數據,你的代碼是什麼,它做了什麼,你打算取得什麼。 然後幫助你更容易。 總之,這裏是我嘗試幫助您:

text = 'blabla word1 word2 WORD word book BOOK bLaBlA' 
unique_words = list(set(text.lower().split(' '))) 
print('text : %s' % text) 
print('unique words : %s' % ','.join(unique_words)) 

編輯:

def analyse_sentence(): 
    sentence = input("Please enter your sentence") 
    unique_words = list(set(sentence.lower().split(' '))) 
    print('sentence : %s' % sentence) 
    print('unique words : %s' % ','.join(unique_words)) 
    return unique_words 
+0

我已將代碼的開頭放在說明中 – Rebecca

+0

這只是以相反的順序返回我的句子,並且每個單詞之間都帶有點。我輸入了「你好,爲什麼不是」這個詞,它把這個獨特的詞語歸還爲:not.why.hello。 A – Rebecca

+0

這是正常的,你的句子是由獨特的單詞構成的。 嘗試:你好你好HELLO爲什麼不是爲什麼,你會看到它只返回唯一的單詞。 –

相關問題