What’s New in Python 2.7 — Python 3.4.0b2 documentation
2014-01-22 10:21
471 查看
What’s New in Python 2.7 — Python 3.4.0b2 documentation
The OrderedDict API provides the same interface as regular dictionaries but iterates over keys and values in a guaranteed order depending on when a key was first inserted:
>>>
>>>
>>>
>>>
>>>
PEP 372: Adding an Ordered Dictionary to collections¶
Regular Python dictionaries iterate over key/value pairs in arbitrary order. Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, 2.7 introduces a new OrderedDict class in the collections module.The OrderedDict API provides the same interface as regular dictionaries but iterates over keys and values in a guaranteed order depending on when a key was first inserted:
>>>
>>> from collections import OrderedDict >>> d = OrderedDict([('first', 1), ... ('second', 2), ... ('third', 3)]) >>> d.items() [('first', 1), ('second', 2), ('third', 3)]If a new entry overwrites an existing entry, the original insertion position is left unchanged:
>>>
>>> d['second'] = 4 >>> d.items() [('first', 1), ('second', 4), ('third', 3)]Deleting an entry and reinserting it will move it to the end:
>>>
>>> del d['second'] >>> d['second'] = 5 >>> d.items() [('first', 1), ('third', 3), ('second', 5)]The popitem() method has an optional last argument that defaults to True. If last is True, the most recently added key is returned and removed; if it’s False, the oldest key is selected:
>>>
>>> od = OrderedDict([(x,0) for x in range(20)]) >>> od.popitem() (19, 0) >>> od.popitem() (18, 0) >>> od.popitem(last=False) (0, 0) >>> od.popitem(last=False) (1, 0)Comparing two ordered dictionaries checks both the keys and values, and requires that the insertion order was the same:
>>>
>>> od1 = OrderedDict([('first', 1), ... ('second', 2), ... ('third', 3)]) >>> od2 = OrderedDict([('third', 3), ... ('first', 1), ... ('second', 2)]) >>> od1 == od2 False
相关文章推荐
- What’s New in Python 2.7 — Python 3.4.0b2 documentation
- What’s New In Python 3.0
- 一入python深似海--what's new in python 3.0
- What's New in Python3.0
- What’s New In “Dive Into Python 3” Python 3中的新玩意儿
- What's New in Oracle WebLogic Server 11g Release 1 (10.3.4)
- Python version 2.7 required, which was not found in the registry
- Python Version 2.7 required which was not found in the registry
- 安装 pywin32-218.win32-py2.7.exe 报错python version 2.7 required,which was not found in the registry解决方案
- Python version 2.7 required, which was not found in the registry
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- 安装PIL遇到python version 2.7 required, which was not found in the registry解决办法
- What’s New in ASP.NET MVC 2
- SharePoint 2016 的新特性概览(一)(What's New for IT Professionals in SharePoint Server 2016)
- Python version 2.7 required, which was not found in the registry
- what is new in c sharp 2.0--study from msdn [转]
- What's new in Xcode 8
- What's New in the C# 3.0 Language and Compiler
- What's New in the .NET Framework 2.0 Beta