我是今年开始编写代码的初学者,我真的希望我能继续前进并了解将来如何处理一些真正困难的任务。但是,我对此感到非常困惑。我正在编写一个代码,该代码将计算输入中单词的实例,并将其简单地放入列表中,而不进行任何形式的排序。这将是一些非常原始的代码,但是我认为它将正确地完成它的简单工作,但是,它似乎想在输入中间始终以一个“ a”开头,而没有明显的原因。为什么会这样?
TLDR: 为什么我的代码在字典的开头排序一个“ a”?
代码:
wordDict = {}
text = input("Enter some text: ")
text = text.lower()
text = text.split()
print text
for word in text:
if word in wordDict:
wordDict[word] += 1
else:
wordDict[word] = 1
print wordDict
感谢您的帮助和时间:)
编辑:添加图片
答案 0 :(得分:0)
在评论之后,尝试以下操作:
from collections import OrderedDict
wordDict = OrderedDict()
text = input("Enter some text: ")
text = text.lower()
text = text.split()
print text
for word in text:
if word in wordDict:
wordDict[word] += 1
else:
wordDict[word] = 1
print wordDict