Python使字典记录其键值对添加的顺序
要用到的模块
collections.py
需要用到的OrderedDict类
例如:
from collections import OrderedDict
"""记录字典键值对添加的顺序"""
#调用OrderedDict创建一个有序的空字典
dicts = OrderedDict()
i = 0
while True:
value = input("value is :")
if value == "exit" or value == "quit":
break
key = str(i + 1)
i += 1
dicts[key] = value
dicts = dict(dicts)
print(dicts)