簡體   English   中英

隨機化Python OrderedDict的順序

[英]Randomize order of Python OrderedDict

samplesDict是OrderedDict對象的samplesDict 來自Python集合 對於每個OrderedDict,我想創建一個副本,其中訂單是隨機的。

import collections
import copy
import random
...
randomizedSamplesDict = copy.deepcopy(samplesDict)
for k, i in samplesDict.iteritems():
  random.shuffle(i)

但是我一直收到random.shuffle(i) KeyError: 56random.shuffle(i)行上是KeyError: 56 誤差整數(例如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'])])

要隨機化一個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'])])

我不確定您要在這里做什么? 但這是獲得最終結果的一種方法:

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()])

如果您只想從排序的字典中隨機獲得項目的順序,請將其轉換為普通字典; 然后隨機播放鍵。

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)])

如您所見,第二個dict(通過鍵)被拖曳了。

使用您的數據,我得到輸出:

...
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'])])

您洗錯了項目,應該遍歷randomizedSamplesDict

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

您已經對samplesDict進行了samplesDict因此,如果您要在randomizedSamplesDictsamplesDict順序,則需要迭代該字典而不是原始字典。

如果您實際上想要一個隨機字典中帶有來自samplesDict的鍵和值的新字典,則在改組后使用原始字典中的項來創建新的OrderedDict:

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))
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

在Python 3.6+中,字典是有序的,因此以前起作用的解決方案可能會產生類似TypeError: 'odict_items' object does not support indexing錯誤TypeError: 'odict_items' object does not support indexing

相反,請嘗試:

items = list(my_dict.items())
random.shuffle(items)
my_dict = OrderedDict(items)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM