我需要帮助试图把输入的字符串转换成字典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
我需要它,因此字典中的数字没有输入 –