1. 程式人生 > >python字典保持有序

python字典保持有序

ordereddict


"""
使用collections.OrderedDict(有序字典)
以OrderedDict替代內置字典Dict,依次將選手成績存入OrderedDict
"""
from collections import OrderedDict

d = OrderedDict()
d[‘aaa‘] = (1, 45)
d[‘bbb‘] = (2, 47)
d[‘ccc‘] = (3, 50)print d

demo:

from collections import OrderedDict
from time import time
from random import randint

player = list(‘ABCDEFGH‘)start = time()for i in xrange(8):
    raw_input()  # 程序阻塞,系統時間仍執行
    p = player.pop(randint(0, 7 - i))    end = time()
    print i + 1, p, end - start
    d[p] = (i + 1, end - start)for k in d:
    print k, d[k]


本文出自 “13261842” 博客,請務必保留此出處http://13271842.blog.51cto.com/13261842/1967666

python字典保持有序