python random dict key,并访问它 [英] python random dict key, and access it
本文介绍了python random dict key,并访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import random
Cards = {
"Spade": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"],
"Diamond": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"],
"Club": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"],
"Heart": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
}
for _ in range(55):
r = random.sample(list(Cards), 1)
print(r[0])
我尝试了很多,无法解决.我在访问密钥的值时遇到问题
I've tried plenty more and couldn't figure it out. I'm having problem accessing the value of the key
推荐答案
像这样吗?
>>> random.choice(list(Cards))
'Heart'
然后选择一张随机卡:
>>> suit = random.choice(list(Cards))
>>> card = random.choice(Cards[suit])
>>> card, suit
('King', 'Heart')
这篇关于python random dict key,并访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文