我试图计算我收集的一些演讲中出现口头收缩的次数。一个特殊的演讲是这样的:从列表中计算字符串中元素的出现次数?
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