随机排列Python OrderedDict的顺序
samplesDict是OrderedDict对象的默认Dict;从…起对于每个OrderedDict,我想创建一个随机排列顺序的副本随机排列Python OrderedDict的顺序,python,random,ordereddictionary,Python,Random,Ordereddictionary,samplesDict是OrderedDict对象的默认Dict;从…起对于每个OrderedDict,我想创建一个随机排列顺序的副本 import collections import copy import random ... randomizedSamplesDict = copy.deepcopy(samplesDict) for k, i in samplesDict.iteritems(): random.shuffle(i) 但我一直得到一个关键错误:随机56个;错误整数(
import collections
import copy
import random
...
randomizedSamplesDict = copy.deepcopy(samplesDict)
for k, i in samplesDict.iteritems():
random.shuffle(i)
但我一直得到一个关键错误:随机56个;错误整数(例如56)每次都不同
为了举例说明,其中一个OrderedPicts可能是
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'])])
我真的不知道你在这里想做什么;但有一种方法可以让你得到最终的结果:
ordered_dicts = samplesDict.values()
现在,如果要随机获取已排序的dict:
random.choice(ordered_dict)
the_dict_you_want = 1
random.choice([value for value in ordered_dicts[the_dict_you_want].values()])
如果要从其中一个有序DICT随机获取值:
random.choice(ordered_dict)
the_dict_you_want = 1
random.choice([value for value in ordered_dicts[the_dict_you_want].values()])
如果您只想从已排序的dict中随机排序项目,请将它们转换为普通dict;然后洗牌
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上迭代: 您已经制作了samplesDict的深度副本,因此如果要在randomizedSamplesDict中无序排列顺序,您需要迭代该dict,而不是原始dict 如果您确实想要一个随机顺序的带有SampleDictin中的键和值的新dict,请使用原始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))
要随机化有序数据,可以执行以下操作:
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'])])
在Python3.6+中,字典是有序的,因此以前使用过的解决方案可能会产生诸如TypeError之类的错误:“odict_items”对象不支持索引 相反,请尝试:
items = list(my_dict.items())
random.shuffle(items)
my_dict = OrderedDict(items)
什么是随机化的顺序?你的问题没有意义,除非你解释你试图用这个来完成什么。我得到了TypeError:“odict_items”对象不支持索引可能这是因为字典现在已经排序了?