python_ordered_dict
目次
OrderedDict
since 2013-02-10
dict だと for で順番どおりに取り出せない:
files = { 'DURATION_PDF': ('dur.pdf',), 'DURATION_TREE': ('tree-dur.inf',), 'STREAM_WIN[MCP]': ('mgc.win1', 'mgc.win2', 'mgc.win3',), } for key, value in files.iteritems(): for f in value: print 'writing %s' % f
>python dict.py writing mgc.win1 writing mgc.win2 writing mgc.win3 writing tree-dur.inf writing dur.pdf
OrderedDict を使うと順番どおりに取り出せる:
from collections import OrderedDict files = OrderedDict([ ('DURATION_PDF', ('dur.pdf',)), ('DURATION_TREE', ('tree-dur.inf',)), ('STREAM_WIN[MCP]', ('mgc.win1', 'mgc.win2', 'mgc.win3',)), ]) for key, value in files.iteritems(): for f in value: print 'writing %s' % f
>python ordered_dict.py writing dur.pdf writing tree-dur.inf writing mgc.win1 writing mgc.win2 writing mgc.win3
python_ordered_dict.txt · 最終更新: 2013/02/10 23:10 by Takuya Nishimoto