们自己写的字典只能保存输入的键值对,但是不能保存他们输入的顺序,这时候可以借助Python的标准库collections里的orderedDict类
from collections import OrderedDict my_ordered_diction = OrderedDict() my_ordered_diction['wukong'] = 'C++' my_ordered_diction['bajie'] = 'Java' my_ordered_diction['shaseng'] = 'Qt' my_ordered_diction['tangsheng'] = 'python' for name,laugage in my_ordered_diction.items(): print(name.title() + ' favorite laugage is ' + laugage.title())