【发布时间】:2012-09-10 08:32:47
【问题描述】:
我正在制作一个简单的闪存卡程序来建立我的词汇量,因此需要随机化一个字典 我在这个网站上找到了很多有用的答案,在这个网站上找到了很多,并注意到随机播放是一种选择(我知道有 random.choice,但我选择了随机播放)但有些东西我没有掌握。看起来像这样:
import random
vocab_dict = {'word1':'def1',
'word2':'def2',
'etc.':'etc.'}
vocab_dict_rand = vocab_dict.keys()
random.shuffle(vocab_rand) **#<--This is where problem occurs**
for key in vocab_rand:
print word
input ""
print definition
input ""
input exit
因此程序崩溃并使用 random.shuffle(vocab_rand)... 指向该行。
我在 shell 中得到了这个
File "C:\Python31\lib\random.py", line 270, in shuffle
x[i], x[j] = x[j], x[i]
TypeError: 'dict_keys' object does not support indexing
任何帮助将不胜感激。
【问题讨论】:
-
您覆盖了编辑之前保存的格式编辑。
标签: python dictionary typeerror