开发者

Looping Python dictionaries in entered sequence

I am new in Python 开发者_如何学Pythonand while looping through the items of a dictionary structure I discovered that they come up in a different order than the sequence they were entered. That happens in Perl hashes too I think. Is there a simple way to get the items in the "right" order?


Dictionaries are inherently unordered; it's what gives them (amortized) O(1) lookups for keys.

In more recent versions of python, there is collections.OrderedDict which does preserve insertion order. Or, you could just keep a separate list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜