import itertools
ranks = '23456789TJQKA'
deck = [r+s for r in ranks for s in 'dchs']
allholdemhands = [i for i in itertools.combinations(deck,2)]
def allcombos(hand,group):
'give all combos of a given hand for a given game'
return [i for i in group if (i[0][0] == hand[0] and i[1][0] == hand[1])
or (i[0][0] == hand[1] and i[1][0] == hand[0])]
def allbettercombos(cards,group):
'all combos as good as or better, of that type'
if cards[0] == cards[1]:
betterpairs = [i+i for i in ranks if ranks.index(i) >=
ranks.index(cards[0])]
return [allcombos(x,group) for x in betterpairs]
else:
bettercards = [cards[0]+i for i in ranks
if ranks.index(i) >= ranks.index(cards[1])
and ranks.index(i) < ranks.index(cards[0])]
return [allcombos(x,group) for x in bettercards]
def makerange(hands, group = allholdemhands):
'create a range of all hands from list of types'
handrange = []
for hand in hands:
if hand[-1] != '+':
handrange.append(allcombos(hand,group))
else:
handrange.append(allbettercombos(hand,group))
return handrange
Above is code to allow the makerange function to take as input some set of holdem poker hands or ranges and output a list of every possible unique card combination there is.
It works but the output comes out as a list, length 1 and I would like it to be a list with a length equal to the number of combinations of cards it represents.
- Can someone tell me how to change the code so the below output would be a list of length 38, with each hand as an element?
PS: a quick primer on the syntax of the input for makerange:
makerange(['AA','KJ+']) would output:
[[('Ad', 'Ac'), ('Ad', 'Ah'), ('Ad', 'As'), ('Ac', 'Ah'), ('Ac', 'As'), ('Ah', 'As')], [[('Td', 'Kd'), ('Td', 'Kc'), ('Td', 'Kh'), ('Td', 'Ks'), ('Tc', 'Kd'), ('Tc', 'Kc'), ('Tc', 'Kh'), ('Tc', 'Ks'), ('Th', 'Kd'), ('Th', 'Kc'), ('Th', 'Kh'), ('Th', 'Ks'), ('Ts', 'Kd'), ('Ts', 'Kc'), ('Ts', 'Kh'), ('Ts', 'Ks')], [('Jd', 'Kd'), ('Jd', 'Kc'), ('Jd', 'Kh'), ('Jd', 'Ks'), ('Jc', 'Kd'), ('Jc', 'Kc'), ('Jc', 'Kh'), ('Jc', 'Ks'), ('Jh', 'Kd'), ('Jh', 'Kc'), ('Jh', 'Kh'), ('Jh', 'Ks'), ('Js', 'Kd'), ('Js', 'Kc'), ('Js', 'Kh'), ('Js', 'Ks')], [('Qd', 'Kd'), ('Qd', 'Kc'), ('Qd', 'Kh'), ('Qd', 'Ks'), ('Qc', 'Kd'), ('Qc', 'Kc'), ('Qc', 'Kh'), ('Qc', 'Ks'), ('Qh', 'Kd'), ('Qh', 'Kc'), ('Qh', 'Kh'), ('Qh', 'Ks'), ('Qs', 'Kd'), ('Qs', 'Kc'), ('Qs', 'Kh'), ('Qs', 'Ks')]]]
This represents all 6 ways to make AA and all 32 hands that have a high card K and at least a J as the second highest.