2015-07-12 99 views
0

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: 56random.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'])]) 
+0

什么是随机的顺序?你的问题没有意义,除非你解释你试图用这个完成什么。 –

回答

0

我真的不知道你正试图在这里做的;但这里是让你最终结果的一种方法:

ordered_dicts = samplesDict.values() 

现在,如果你想随机取有序字典:

random.choice(ordered_dict) 

如果你想从有序类型的字典的一个取一个值随机:

the_dict_you_want = 1 
random.choice([value for value in ordered_dicts[the_dict_you_want].values()]) 

如果您只是想从订购的字典中随机地订购商品,请将它们转换为正常的字典;然后洗牌的钥匙。

0
import collections 
import copy 
import random 

samplesDict = collections.OrderedDict() 
samplesDict[1] = 10 
samplesDict[2] = 20 
samplesDict[3] = 30 
print samplesDict 

keys = samplesDict.keys() 
random.shuffle(keys) 
print keys 

randomizedSamplesDict = collections.OrderedDict() 
for k in keys : 
    randomizedSamplesDict[k] = samplesDict[k] 
print randomizedSamplesDict 

输出:

OrderedDict([(1, 10), (2, 20), (3, 30)]) 
[2, 1, 3] 
OrderedDict([(2, 20), (1, 10), (3, 30)]) 

正如你可以看到第二字典被混洗(被键)。

使用你的数据,我得到的输出:

... 
samplesDict = collections.OrderedDict([ 
    ('This is the first key', ['foo', 'baz']), 
    ('And the second key', ['buz', 'baz']), 
    ('Finally the third key', ['bar', 'foo'])]) 
... 

OrderedDict([('This is the first key', ['foo', 'baz']), 
('And the second key', ['buz', 'baz']), 
('Finally the third key', ['bar', 'foo'])]) 

['And the second key', 'Finally the third key', 'This is the first key'] 

OrderedDict([('And the second key', ['buz', 'baz']), 
('Finally the third key', ['bar', 'foo']), 
('This is the first key', ['foo', 'baz'])]) 
0

你洗牌了错误的物品,你应该遍历randomizedSamplesDict

randomizedSamplesDict = copy.deepcopy(samplesDict) 
for k, i in randomizedSamplesDict.items(): 
    random.shuffle(i) 
print(randomizedSamplesDict) 

你已经做了的samplesDict一个deepcopy的,所以如果你想要洗牌randomizedSamplesDict中的订单,您需要对该字典进行迭代而不是原始的。

如果你确实想以随机的顺序使用从原来的字典项洗牌创建一个新的OrderedDict后的按键和samplesDict值的新字典:

import collections 
import copy 
import random 
samplesDict = collections.OrderedDict([ 
    ('This is the first key', ['foo', 'baz']), 
    ('And the second key', ['buz', 'baz']), 
    ('Finally the third key', ['bar', 'foo'])]) 

items = list(samplesDict.items()) 
random.shuffle(items) 
randomizedSamplesDict = collections.OrderedDict(copy.deepcopy(items)) 
0

要随机化OrderedDict可以做到以下几点:

od = collections.OrderedDict([('This is the first key', ['foo', 'baz']), ('And the second key', ['buz', 'baz']), ('Finally the third key', ['bar', 'foo'])]) 
items = od.items() 
random.shuffle(items) 
odrnd = collections.OrderedDict(items) 
ornd 
OrderedDict([('Finally the third key', ['bar', 'foo']), ('This is the first key', ['foo', 'baz']), ('And the second key', ['buz', 'baz'])]) 
0
import random 
from collections import defaultdict, OrderedDict 

randomizedSamplesDict = defaultdict(list) 
for category, samples in samplesDict.iteritems(): 
    # samples is an OrderedDict object 
    keys = samples.keys() 
    random.shuffle(keys) 
    tempOrderedDict = OrderedDict() 
    for k in keys: 
    tempOrderedDict[k] = samples[k] 
    randomizedSamplesDict[category] = tempOrderedDict 

return randomizedSamplesDict