2015-07-12 200 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