字典中的FIFO顺序的pythonic方式 [英] pythonic way for FIFO order in Dictionary

查看:104
本文介绍了字典中的FIFO顺序的pythonic方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中填写一个字典,但是我想保留键入的顺序,就像一个列表一样,就像FIFO一样。



例如,



我读了一个名为animals.txt的文件,其中包含以下信息:

  animal\tconservation_status\\\

dog\tdomesticated\\\

tiger\tEN\\\

panda\tEN\\\

即,

  animals = {'dog':'dom','tiger':'EN','panda':'EN'} 
>>>对于动物的el:
... print el
...
tiger
dog
熊猫

在FIFO中应该是狗,老虎,熊猫出来...



当我将其读入字典,订单不会被保留。我希望订单被保留,以便当我做一个循环时,FIRST IN是FIRST OUT。



Ie,
dog,then tiger,然后熊猫。



有没有一个很好的方式来做,而不必保留外部索引或更复杂的字典结构?不确定,对不起,我的天真在这里...

解决方案

是的。您使用 collections.OrderedDict 而不是常规字典。

 >>> d = OrderedDict((x,x)for x in revers(range(10)))
>>>> d
OrderedDict([(9,9),(8,8),(7,7),(6,6),(5,5),(4,4),(3,3) (2,2),(1,1),(0,0)])
>>>正则= dict((x,x)for x in revers(range(10)))
>>>>常规
{0:0,1:1,2:2,3:3,4:4,5,5,6:6,7:7,8:8,9:9}

请注意, OrderedDict 保留订单,而常规 dict 不。






  >>> OrderedDict([('dog','dom'),('tiger','EN'),('panda','EN')])
OrderedDict([('dog','dom' ,('tiger','EN'),('熊猫','EN')])



.update )。换句话说,您不能将关键字参数传递给构造函数,并希望保留订单:

 >> ; OrderedDict(dog ='dom',tiger ='EN',熊猫='EN')#不保存订单
OrderedDict([('tiger','EN'),('熊猫' '),('dog','dom')])


I am trying to populate a dictionary in python but I would like to preserve the order of the keys as they get in - exactly FIFO like a list would do it.

For example,

I read a file called animals.txt containing the following information:

animal\tconservation_status\n
dog\tdomesticated\n
tiger\tEN\n
panda\tEN\n

I.e.,

animals = {'dog':'dom','tiger':'EN', 'panda':'EN'}
>>> for el in animals:
...     print el
... 
tiger
dog
panda

And in a FIFO SHOULD have been dog, tiger, panda as they come out...

When I read it into a dictionary the order will not be preserved. I would like the order to be preserved such that when I do a for loop the FIRST IN is the FIRST OUT.

I.e., dog, then tiger, then panda.

Is there a nice way to do this without having to keep an external index or a more complex dictionary structure? Not sure, sorry for my naivity here...

解决方案

Yes. You use a collections.OrderedDict instead of a regular dictionary.

>>> d = OrderedDict((x,x) for x in reversed(range(10)) )
>>> d
OrderedDict([(9, 9), (8, 8), (7, 7), (6, 6), (5, 5), (4, 4), (3, 3), (2, 2), (1, 1), (0, 0)])
>>> regular = dict((x,x) for x in reversed(range(10)))
>>> regular
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}

Notice that the OrderedDict preserves the order whereas the regular dict does not.


>>> OrderedDict([('dog','dom'),('tiger','EN'), ('panda','EN')])
OrderedDict([('dog', 'dom'), ('tiger', 'EN'), ('panda', 'EN')])

Another gotcha is that you need to pass items to the constructor (or .update) in a way that preserves order. In other words, you can't pass keyword args to the constructor and expect order to be preserved:

>>> OrderedDict(dog='dom',tiger='EN',panda='EN')  #doesn't preserve order
OrderedDict([('tiger', 'EN'), ('panda', 'EN'), ('dog', 'dom')])

这篇关于字典中的FIFO顺序的pythonic方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆