Python让字典保持有序

Jackey Python 1,834 次浏览 , 没有评论
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 让字典保持有序
from mrjob.py2 import xrange
from pip._vendor.distlib.compat import raw_input
d = {}
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)
for k in d:
print(k)
from collections import OrderedDict
d = OrderedDict()
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)
for k in d:
print(k)
from time import time
from random import randint
d = OrderedDict()
players = list('ABCDEFGH')
start = time()
for i in xrange(8):
raw_input()
p = players.pop(randint(0, 7 - i))
end = time()
print(i + 1, p, end - start)
d[p] = (i + 1, end - start)
print()
print('-' * 20)
for k in d:
print(k, d[k])
# 让字典保持有序 from mrjob.py2 import xrange from pip._vendor.distlib.compat import raw_input d = {} d['Jim'] = (1, 35) d['Leo'] = (2, 37) d['Bob'] = (3, 40) for k in d: print(k) from collections import OrderedDict d = OrderedDict() d['Jim'] = (1, 35) d['Leo'] = (2, 37) d['Bob'] = (3, 40) for k in d: print(k) from time import time from random import randint d = OrderedDict() players = list('ABCDEFGH') start = time() for i in xrange(8): raw_input() p = players.pop(randint(0, 7 - i)) end = time() print(i + 1, p, end - start) d[p] = (i + 1, end - start) print() print('-' * 20) for k in d: print(k, d[k])
# 让字典保持有序
from mrjob.py2 import xrange
from pip._vendor.distlib.compat import raw_input

d = {}
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)

for k in d:
    print(k)

from collections import OrderedDict
d = OrderedDict()
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)
for k in d:
    print(k)

from time import time
from random import randint

d = OrderedDict()
players = list('ABCDEFGH')
start = time()
for i in xrange(8):
    raw_input()
    p = players.pop(randint(0, 7 - i))
    end = time()
    print(i + 1, p, end - start)
    d[p] = (i + 1, end - start)
print()
print('-' * 20)
for k in d:
    print(k, d[k])

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Go