samplesDict
是OrderedDict对象的默认用户;来自Python collections。对于每个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'])])
答案 0 :(得分:0)
我不确定你在这里要做什么;但这是获得最终结果的一种方法:
ordered_dicts = samplesDict.values()
现在,如果你想随机获取有序的字典:
random.choice(ordered_dict)
如果你想从一个有序的dicts中随机获取一个值:
the_dict_you_want = 1
random.choice([value for value in ordered_dicts[the_dict_you_want].values()])
如果您只想从订购的dicts中获得随机的项目顺序,请将它们转换为正常的dicts;然后洗牌。
答案 1 :(得分: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'])])
答案 2 :(得分:0)
你正在改组错误的项目,你应该迭代randomizedSamplesDict
:
randomizedSamplesDict = copy.deepcopy(samplesDict)
for k, i in randomizedSamplesDict.items():
random.shuffle(i)
print(randomizedSamplesDict)
您已对samplesDict
进行了深入复制,因此如果您要在randomizedSamplesDict
中对顺序进行随机播放,则需要迭代该字典而不是原始字典。
如果你真的想要一个带有来自samplesDict
的密钥和值的新dict,那么在洗牌之后使用原始字典中的项来创建一个新的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))
答案 3 :(得分: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'])])
答案 4 :(得分: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
答案 5 :(得分:0)
在Python 3.6+中,字典是有序的,因此以前有效的解决方案可能会产生诸如TypeError: 'odict_items' object does not support indexing
之类的错误。
相反,请尝试:
items = list(my_dict.items())
random.shuffle(items)
my_dict = OrderedDict(items)