葫芦(Full house)
让我们回到扑克牌游戏。上一次,您计算了得到至少一对(两张点数相同的牌)的概率。这一次我们关注葫芦(Full house)。所谓葫芦,是指您拿到两张点数相同、花色不同的牌,以及另外三张点数相同的牌(例如:红心 2 与黑桃 2,加上梅花 J、方块 J、黑桃 J)。
因此,葫芦可以看作是在已经恰好有一对的条件下,再恰好有三张同点数牌的事件的概率。请在沿用之前代码的基础上,修改"成功条件",以得到所需输出。通过本练习,您将学会在纸牌游戏中估计条件概率,并为用模拟来刻画抽象问题打下基础。
本练习是课程的一部分
Python 中的统计模拟
练习说明
- 洗牌
deck_of_cards。 - 使用带有
.get()的字典来统计手牌中各点数出现的次数。 - 当手牌为葫芦(2 张同点数 + 3 张同点数)时,递增计数器
full_house。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Shuffle deck & count card occurrences in the hand
n_sims, full_house, deck_of_cards = 50000, 0, deck.copy()
for i in range(n_sims):
____
hand, cards_in_hand = deck_of_cards[0:5], {}
for card in hand:
# Use .get() method to count occurrences of each card
cards_in_hand[card[1]] = cards_in_hand.____(card[1], 0) + 1
# Condition for getting full house
condition = (max(cards_in_hand.values()) ==3) & (min(cards_in_hand.values())==2)
if condition:
full_house ____
print("Probability of seeing a full house = {}".format(full_house/n_sims))