我試圖用代碼來開發一個馬爾科夫鏈,從一個字串中創建隨機句子。我已經對字串進行了分割,并正在對其進行評估。我目前的代碼是:
#你的代碼在這里。
string = '''一只木頭人可以夾多少木頭?
如果一只木頭人可以吸食木頭?
一個木頭人可以夾多少木頭?
如果一個木頭人可以吸食木頭的話。''
st_dict={}
for i in range(0, len(words)-1)。)
#print(words): #print(words).
word=words[i]
next_word=words[i 1]
if word in ['. ','? ']:
st_dict[word]=[]
elif word in st_dict:
st_dict[word].append(next_word)
else:
st_dict[word]=[Next_word]
st_dict[words[-1]]=[] 。
print(st_dict)
然而,任何含有標點符號的詞都應該只有一個空串列作為其值。然而,我不能讓它作業。我試過了。 word not in ['.', '?'......等] 除了上面的if陳述句外,但在字串中間有標點符號的單詞仍然附加下一個單詞作為值。我怎樣才能防止這種情況呢?
謝謝。
uj5u.com熱心網友回復:
你想要相反的結果。word in ['.','?']只有在word是確切地'.'或確切地'?
'.'在單詞中或'?'在單詞中.
如果你有一個標點符號的串列,那么你應該使用any:
punctuation = ".?"
any(punct in word for punct in標點)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/328616.html
標籤:
上一篇:在一個嵌套回圈中更新字典
下一篇:img資料型別=17不被支持
