码迷,mamicode.com
首页 > 编程语言 > 详细

python字典保持有序

时间:2017-09-22 11:57:34      阅读:141      评论:0      收藏:0      [点我收藏+]

标签: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字典保持有序

标签:ordereddict

原文地址:http://13271842.blog.51cto.com/13261842/1967666

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!