如何做一个字典,用fifo程序给输出 [英] how to do a dictionary that give the output with a fifo process
问题描述
我正在搜索使用允许方法的字典的方法或库,例如: element_dict_in({'polo':789})
和 element_dict_out()
返回我放在字典中的第一个关系,我之前提到的2种方法是尚未实施,这是为了澄清我的想法:
i am searching a method or library that use a dict that allow method like:
element_dict_in({'polo':789})
,and element_dict_out()
that return me the first relation that was put in the dictionary, the 2 method that i mentioned before are not implemented,it is for clarify my idea:
例如:
dict={}
element_dict_in({'polo1':789})
element_dict_in({'polo2':123})
element_dict_in({'polo3':4556})#{'polo1':789,'polo2':123,'polo3':4556}
element_dict_out()#return {'polo1':789}
我找到此链接 Python字典中FIFO顺序的方式但是对我来说还不够清楚,所以存在这样的东西吗?
i find this link pythonic way for FIFO order in Dictionary but for my it is not enough clear, so exist something like that?
推荐答案
Python actually already has this in the standard library - collections.OrderedDict
.
from collections import OrderedDict
my_dict = OrderedDict()
my_dict['polo1'] = 789
my_dict['polo2'] = 123
my_dict['polo3'] = 4556
print(my_dict.popitem(last=False))
# ('polo1', 789)
Notably, the built-in dict
type can do LIFO popping but not FIFO popping if that's acceptable to you, and is generally faster than the OrderedDict
for most things.
这篇关于如何做一个字典,用fifo程序给输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!