1

假设我有以下变量:

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']

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

4

2 回答 2

2
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']
于 2013-04-01T06:50:21.853 回答
0

你需要类似的东西

In [5]: deck = []

In [6]: for i in itertools.combinations(suits, 2):
   ...:     for j in cards:
   ...:         deck.append(j+i[0]+j+i[1])
   ...:

In [7]: print deck
['2h2c', '3h3c', '4h4c', '5h5c', '6h6c', '7h7c', '8h8c', '9h9c', 'thtc', 'jhjc', 'qhqc', 'khkc', 'ahac',
', '3h3d', '4h4d', '5h5d', '6h6d', '7h7d', '8h8d', '9h9d', 'thtd', 'jhjd', 'qhqd', 'khkd', 'ahad', '2h2s'
3s', '4h4s', '5h5s', '6h6s', '7h7s', '8h8s', '9h9s', 'thts', 'jhjs', 'qhqs', 'khks', 'ahas', '2c2d', '3c3
4c4d', '5c5d', '6c6d', '7c7d', '8c8d', '9c9d', 'tctd', 'jcjd', 'qcqd', 'kckd', 'acad', '2c2s', '3c3s', '4
 '5c5s', '6c6s', '7c7s', '8c8s', '9c9s', 'tcts', 'jcjs', 'qcqs', 'kcks', 'acas', '2d2s', '3d3s', '4d4s',
', '6d6s', '7d7s', '8d8s', '9d9s', 'tdts', 'jdjs', 'qdqs', 'kdks', 'adas']
于 2013-04-01T06:50:52.243 回答