samplesDict
是OrderedDict對象的defaultdict;從Python collections。對於每個OrderedDict,我想創建一個訂單隨機化的副本。Python的隨機順序OrderedDict
import collections
import copy
import random
...
randomizedSamplesDict = copy.deepcopy(samplesDict)
for k, i in samplesDict.iteritems():
random.shuffle(i)
但我不斷收到一個KeyError: 56
在random.shuffle(i)
線;錯誤整數(例如56
)每次都不相同。
爲了說明,OrderedDicts之一可能是
OrderedDict([
('This is the first key', ['foo', 'baz']),
('And the second key', ['buz', 'baz']),
('Finally the third key', ['bar', 'foo'])])
而且我想在副本成爲
OrderedDict([
('Finally the third key', ['bar', 'foo']),
('This is the first key', ['foo', 'baz']),
('And the second key', ['buz', 'baz'])])
什麼是隨機的順序?你的問題沒有意義,除非你解釋你試圖用這個完成什麼。 –