python - 如何制作一个字典,通过 fifo 过程给出输出

标签 python dictionary fifo

我正在搜索使用允许以下方法的字典的方法或库: 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/

相关文章:

python - 通过字典获取路径

python - 从 python 字典更新文本文件

javascript - Node.js fs.open() 在尝试打开超过 4 个命名管道 (FIFO) 后挂起

c# - 尝试了解 Azure 服务总线 session

named-pipes - USB 大容量存储小工具上的命名管道/FIFO,用于为汽车、码头等流式传输音频。

python - vscode python linux : changes in debug console doesn't appear in 'variables->locals' debug pane

python - 找不到记录器 "pika.adapters.blocking_connection"的处理程序

java - 如何保留排序映射的值顺序?

Python 日志记录 : Flask + uWsgi + nginx

列表项的python连接组合