我如何以随机顺序迭代字典中的所有项?我的意思是类似于random.shuffle,但是用于字典。
dict
是一个无序的键值对集合。当你迭代一个 dict
时,迭代的顺序是随机的。但是如果想要明确地随机排列键值对的顺序,需要使用另一个有序的对象,比如列表。dict.items()
、dict.keys()
和 dict.values()
分别返回列表,可以进行洗牌。
items=d.items() # List of tuples
random.shuffle(items)
for key, value in items:
print key, value
keys=d.keys() # List of keys
random.shuffle(keys)
for key in keys:
print key, d[key]
或者,如果你不关心键:
values=d.values() # List of values
random.shuffle(values) # Shuffles in-place
for value in values:
print value
你也可以通过“随机排序”:
for key, value in sorted(d.items(), key=lambda x: random.random()):
print key, value
.keys()
获取键的列表,打乱该列表,然后在遍历时使用原始字典的索引。.items()
获取键值对的列表,打乱并在遍历时使用。dict.values()
。 - Michael Hoffmanfor item in random.sample(list(d.values()), len(d)):
- SurpriseDogimport random
def main():
CORRECT = 0
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau',
'Arizona': 'Phoenix', 'Arkansas': 'Little Rock'} #etc... you get the idea of a dictionary
allstates = list(capitals.keys()) #creates a variable name and list of the dictionary items
random.shuffle(allstates) #shuffles the variable
for a in allstates: #searches the variable name for parameter
studentinput = input('What is the capital of '+a+'? ')
if studentinput.upper() == capitals[a].upper():
CORRECT += 1
main()
def shuffled(lis):
for index in random.sample(range(len(lis)), len(lis)):
yield lis[index]
d
:for item in shuffled(list(d.values())):
print(item)
或者如果你想跳过创建一个新函数,这里有一个两行代码的解决方案:
for item in random.sample(list(d.values()), len(d)):
print(item)
正如Charles Brunet所说,字典是键值对的随机排列。但为了使它真正随机,您将使用随机模块。 我编写了一个函数,它将混洗所有的键,因此在您遍历它时,您将会随机遍历。您可以通过查看代码更清楚地理解:
def shuffle(q):
"""
This function is for shuffling
the dictionary elements.
"""
selected_keys = []
i = 0
while i < len(q):
current_selection = random.choice(q.keys())
if current_selection not in selected_keys:
selected_keys.append(current_selection)
i = i+1
return selected_keys
name_of_dictionary[key]
来获取值。numpy.random.permutation()
。但如果你真的想自己做,这似乎是一种非常缓慢的洗牌方式!假设你有一个带有10,000个键的字典,并且你只剩下最后一个键。想象一下,在选择它之前你将不得不经历多少次失败的尝试!为了改进算法,您可以在每次迭代中从候选项中删除每个已选择的键,以便只从剩余的键中进行选择。 - Moot
d.items()
、d.keys()
和d.values()
生成迭代器对象。您需要使用list()
函数显式地将该迭代器转换为列表。 - Charles Brunetnp.random.RandomState(100)
)? - Dhruv Ghulati