所以我有一本字典:
dict = {1:'hi', 2:'bye', 3:'who', 4:'which', 5:'where'}
我正尝试从字典中随机提取3个单词,并添加到列表中。
我尝试过:
import random
lst = []
pick = random.choice(lst.keys())
但是我遇到了错误:
TypeError: 'dict_keys' object does not support indexing
答案 0 :(得分:0)
import random
dict = {1:'hi', 2:'bye', 3:'who', 4:'which', 5:'where'}
random.choice(list(dict .keys()))
nr,text= random.choice(list(dict .items()))
尝试一下?