gpt4 book ai didi

python:如何在一副牌中生成成对的牌

转载 作者:行者123 更新时间:2023-12-01 05:47:02 24 4
gpt4 key购买 nike

假设我有以下变量:

suits = ["h","c", "d", "s"]
cards = ["2","3", "4", "5", "6", "7", "8", "9", "t", "j", "q", "k", "a"]
deck = []

for suit in suits:
for card in cards:
deck.append(str(card+suit))

我想编写一个函数,在给定特定卡片的情况下,生成可能的配对组合。

例如,generatePairs('a') 应返回如下内容:

['ahac','ahad','ahas','acad','acas','adas']

但我不确定如何编写该函数。

最佳答案

In [7]: import itertools

In [8]: c = 'a'

In [9]: ['%s%s%s%s' % (c, s1, c, s2) for (s1, s2) in itertools.combinations(suits, 2)]
Out[9]: ['ahac', 'ahad', 'ahas', 'acad', 'acas', 'adas']

关于python:如何在一副牌中生成成对的牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738884/

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