问题:
Python如何让字典保持有序 ?
解决方案:
使用collections.OrderedDict代替Dict。
验证程序:
from collections import OrderedDict dic = OrderedDict() dic['Jim'] = (1, 35) dic['Leo'] = (2, 37) dic['Bob'] = (3, 40) for k in dic: print(k)
验证结果:
Jim
Leo
Bob
问题:
Python如何让字典保持有序 ?
解决方案:
使用collections.OrderedDict代替Dict。
验证程序:
from collections import OrderedDict dic = OrderedDict() dic['Jim'] = (1, 35) dic['Leo'] = (2, 37) dic['Bob'] = (3, 40) for k in dic: print(k)
验证结果:
Jim
Leo
Bob