【发布时间】:2015-09-21 23:27:12
【问题描述】:
我正在尝试测试一手五张牌,看看它是否包括四张牌。目前我有两个函数,convert(x) 和draw_n(n)。它们被定义为:
def convert(x):
card = x % 13
suit = 'SHDC'[x/13]
return card, suit, str([card, suit])
def draw_n(n):
from random import sample
# initialize the list
cards = []
# make sure that we have a valid number
if n > 0 and n <= 52:
# sample without replacement
for x in sample(xrange(0, 52), n):
# append the converted card to the list
cards.append(convert(x))
return cards
当使用参数5 执行draw_n(n) 时(即抽5张牌),它会返回一个包含5张随机牌的列表,如下所示:
[(8, 'D', '9 of Diamonds'),
(0, 'H', 'Ace of Hearts'),
(8, 'H', '9 of Hearts'),
(10, 'S', 'Jack of Spades'),
(12, 'C', 'King of Clubs')]
数字是指牌的编号(即0 = Ace,...,12 = King),字母是花色,字符串是牌的名称。
我将在 Python 中多次执行此函数,从而产生多个 5 张牌。我希望能够在 5 张牌手的列表中数出其中有 4 张的手牌数量,但我没有任何运气。任何帮助将不胜感激!
【问题讨论】:
-
我曾尝试使用 split 和 strip 来单独获取卡片字符串(即“红心 9”等),但我没有运气。
标签: python playing-cards