我需要幫助試圖把輸入的字符串轉換成字典Python的 - 將一個字符串轉換成字典
輸入:
Hello my name is Bob Hello Hello
實際輸出:
所需的輸出:
{'Hello' : '1', 'my' : '2', 'name' : '3', 'is' : '4', 'Bob' : '5'}
應該只記錄同一個詞一度
我迄今爲止代碼:
s = input("input sentence: ")
file = open("Original.txt", 'w')
file.write(s)
file.write("\n")
file.close()
num = int(1,2,3,4,5)
dictionary = dict(num(s.split()))
file = open("Dictionary.txt", 'a')
file.write(dictionary)
file.close()
f = open("Dictionary.txt", 'r')
print(file.read)
print(s)
試試這個:http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary – Nikita
1.什麼是你到目前爲止的代碼做什麼,以及如何從您的期望有所不同; 2.有什麼可笑的評論? – jonrsharpe
我需要它,因此字典中的數字沒有輸入 –