python之OrderedDict类
# OrderedDict类使用举例 # OrderedDict类的使用与字典相似,不同的是OrderedDict类会记录键值对的添加顺序 from collections import OrderedDict hoppys = OrderedDict() hoppys['mike'] = 'swim' hoppys['tom'] = 'reading' hoppys['lily'] = 'singing' print(hoppys) print('Tom的爱好是:' + hoppys['tom']) """ OrderedDict([('mike', 'swim'), ('tom', 'reading'), ('lily', 'singing')]) Tom的爱好是:reading """ # 遍历字典 for name in hoppys.keys(): print('-', hoppys.get(name)) """ - swim - reading - singing """ for name, hoppy in hoppys.items(): print('-', name.title(), hoppy) """ - Mike swim - Tom reading - Lily singing """