我正在搜索使用允许以下方法的字典的方法或库:
element_dict_in({'polo':789})
和 element_dict_out()
返回我放入字典中的第一个关系,我之前提到的 2 个方法是没有实现,这是为了澄清我的想法:
例如:
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}
我找到这个链接pythonic way for FIFO order in Dictionary但对我来说还不够清楚,所以 存在这样的东西吗?
最佳答案
Python 实际上已经在标准库中包含了这个 - 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)
值得注意的是,内置的 dict
类型 can do LIFO popping but not FIFO popping如果您可以接受,并且在大多数情况下通常比 OrderedDict
更快。
关于python - 如何制作一个字典,通过 fifo 过程给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484400/