【发布时间】:2017-02-17 22:12:52
【问题描述】:
我正在尝试制作这个文件:
c;f
b;d
a;c
c;e
d;g
a;b
e;d
f;g
f;d
变成这样的字典:
{'e': {'d'}, 'a': {'b', 'c'}, 'd': {'g'}, 'b': {'d'}, 'c': {'f', 'e'}, 'f': {'g', 'd'}}.
我现在使用的代码如下:
def read_file(file : open) -> {str:{str}}:
f = file.read().rstrip('\n').split()
answer = {}
for line in f:
k, v = line.split(';')
answer[k] = v
return answer
但它给了我{'f': 'g', 'a': 'c', 'b': 'd', 'e': 'd', 'c': 'e', 'd': 'g'}
我该如何解决?
【问题讨论】:
标签: python string python-3.x dictionary set