我正在學習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