python字典保持有序


"""
使用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]


你可能感兴趣的:(python)