gpt4 book ai didi

python - 字典中随机选取

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:49 25 4
gpt4 key购买 nike

所以我有一本字典:

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

最佳答案

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()))

试试这个?

关于python - 字典中随机选取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com