Reputation: 16526
Suppose that I have the following variables:
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))
I'd like to write a function that, given a specific card, generates the possible combinations of pairs.
For instance, generatePairs('a')
should return something like:
['ahac','ahad','ahas','acad','acas','adas']
but I'm not sure how to approach writing that function.
Upvotes: 1
Views: 613
Reputation: 14864
you require something like
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']
Upvotes: 0
Reputation: 500763
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']
Upvotes: 2