【发布时间】:2014-12-25 23:06:58
【问题描述】:
好的,给我两列
A S
A T
A Z
B F
B G
B P
B U
C D
C P
C R
D M
E H
F S
H U
第一列是点列表,第二列是点的邻居列表。我想把它做成一本字典,这样 {A:'S','T','Z',B:'F','G','P'等}等等。
鉴于文本文件是两列,我尝试过这样做。
edges = open('romEdges.txt')
edgeslist = edges.read().split()
edgeskeys = edgeslist[::2]
edgesvalues = edgeslist[1::2]
dictionary = {}
for items in edgeskeys:
dictionary[items]=[]
dictionary = OrderedDict(sorted(dictionary.items(), key=lambda t: t[0]))
for items in edgeskeys:
if edgeskeys[items]==dictionary[items]:
print()
print(dictionary)
我尝试过制作 2 个列表,1 个键和 1 个值,并尝试将它们与字典等进行比较,但我就是做错了!
必须有一个简单的方法。
请帮忙。
【问题讨论】:
标签: python list dictionary key key-value