如何对字典的元素进行排序:
with open('directory file txt','r') as f:
for line in f:
keys = line.strip().split('.')
value = keys[-1]
for key in reversed(keys[1:-1]):
value = {key: value}
result[keys[0]]=value
现在如果打印键我有这个:
['a','b','c','d','1']
['a','f','e','d','3']
['f','b','c','d','2']
['a','b','c','d','0']
我可以按最后一个元素对每个元素进行排序并将其保存到文件中吗?所以我的输出将是:
a.b.c.d.0
a.b.c.d.1
f.b.c.d.2
a.f.e.d.3