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'])])
什么是随机的顺序?你的问题没有意义,除非你解释你试图用这个完成什么。 –