我試圖計算我收集的一些演講中出現口頭收縮的次數。一個特殊的演講是這樣的:從列表中計算字符串中元素的出現次數?
speech = "I've changed the path of the economy, and I've increased jobs in our own
home state. We're headed in the right direction - you've all been a great help."
所以,在這種情況下,我想計算四(4)個收縮。我有宮縮的列表,這裏有一些最初的幾個術語:
contractions = {"ain't": "am not; are not; is not; has not; have not",
"aren't": "are not; am not",
"can't": "cannot",...}
我的代碼看起來是這樣的,首先:
count = 0
for word in speech:
if word in contractions:
count = count + 1
print count
我不是這個Anywhere入門但是,因爲代碼遍歷每一個字母,而不是整個單詞。
for word in speech.split(''): – Monkpit
我沒有得到你的字典中的值在做什麼,你有一個字典順便說一句btw沒有列表 –
我在我的答案中添加了很多東西應該給你一些額外的。 – colidyre