gpt4 book ai didi

python - 从字典中查找随机选择的键的值

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

我有一个 .py 字典,我想在另一个文件中使用它。我想随机选择一个 key

w_keys = dictionary.dic.keys()

def inf():
return random.choice(list(w_keys))

x = inf()

然后我需要找到所选键的值,

value = dictionary.dic.values()
return dictionary.dic[x] == value

但我不知道该怎么做,而且我的尝试(不出所料)不起作用?

最佳答案

import random

dict = {'A': 'a', 'B': 'b'}

def inf(dict):
return random.choice(list(dict.keys()))

x = dict[inf(dict)]

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

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