製作一個協調程序。我希望它能告訴我一句話是什麼語句,所以如果我有:Python如何打印一個單詞所在的句子?
「Hello world。我的名字是Nathan,我需要Python的幫助,我很困惑,並且有任何幫助。
我希望它能打印每個單詞來自哪個句子。我已經完成了它計算的總次數出現的每個字,並在它旁邊,我需要的句子號碼是從何而來,所以它會顯示:「A」與
a. word {word appearance count:sentence number}
按列表順序工作(如帶有字母的編號列表)。從第一句的一個例子是
a. help {2:2,3}
這是我目前擁有的代碼:
word_counter = {}
sent_num = {}
linenum = 0
wordnum = 0
counter = 0
#not working
for word in f.lower().split('.'):
if not word in sent_num:
sent_num[word] = []
sent_num[word].append(f.find(wordnum))
#working correctly
for word in f.lower().split():
if not word in word_counter:
word_counter[word] = []
#if the word isn't listed yet, adds it
word_counter[word].append(linenum)
for key in sorted(word_counter):
counter += 1
print (counter, key, len(word_counter[key]), len(sent_num[key]))
'linenum'總是'0' –
這是一個廣泛的問題,因爲有很多你的代碼錯誤。沒有完全重寫就很難回答。我不認爲這個答案對後代有用。將問題分成更小的更具體的區塊,閱讀如何創建[mcve],並可能提出一個新問題。 –