我正在学习python,我喜欢在少量代码中完成多少操作,但是我对语法感到困惑。我只是试图遍历字典并打印出每个项目和值。在python字典中迭代和打印单词
这里是我的代码:
words = {}
value = 1
for line in open("test.txt", 'r'):
for word in line.split():
print (word)
try:
words[word] += 1
except KeyError:
#wur you at key?
print("no")
words[word]=1
for item in words:
print ("{",item, ": ", words[item][0], " }")
我当前的打印语句不工作,我无法找到使用多变量大print语句的一个很好的例子。我将如何正确打印?
你说的意思是什么“不工作”?如果你向我们提供了一个[MCVE](http://stackoverflow.com/help/mcve),或者用'test.txt'的内容,或者更好的是用源代码中定义的'words',这将会有所帮助。然后你可以显示预期的和实际的输出。 – abarnert 2014-12-13 03:51:54