python - Making a list of all possible 5 card poker -
i'm trying make list of possible 5 card poker hands use in computations (that may slow preferably speedy). list, wrote following code:
import itertools # possible cards: cards = ['2s', '2h', '2d', '2c', '3s', '3h', '3d', '3c', '4s', '4h', '4d', '4c', '5s', '5h', '5d', '5c', '6s', '6h', '6d', '6c', '7s', '7h', '7d', '7c', '8s', '8h', '8d', '8c', '9s', '9h', '9d', '9c', 'ts', 'th', 'td', 'tc', 'js', 'jh', 'jd', 'jc', 'qs', 'qh', 'qd', 'qc', 'ks', 'kh', 'kd', 'kc', 'as', 'ah', 'ad', 'ac'] hands = [] # collect non-trivial cartesian products element in itertools.product(cards,cards,cards,cards,cards): c1,c2,c3,c4,c5 = element if c1 != c2 or c1!=c3 or c1!=c4 or c1!=c5 or c2 != c3 or c2 != c4 or c2 != c5 or c3 != c4 or c3 != c5 or c4 != c5: hands.append([c1,c2,c3,c4,c5]) # sort elements , delete duplicates x in hands: x.sort() hands = [tuple(x) x in hands] hands = list(set(hands)) # convert hands list hands = [list(x) x in hands] # verify result print(str(len(hands)))
but runs out of memory before it's completed (over 11 gigs of ram). i'm trying use list can exhaustively test against set of possible hands when try put 2 hands against 1 another.
does know how make code better?
first, function trying create exists: itertools.combinations
. second, try structure code can iterate on possible hands without putting them in memory simultaneously.
here short program prints possible hands, duplicate hands removed, never creates in-memory list of possible hands:
import itertools cards = [''.join(x) x in itertools.product('23456789tjqka', 'shdc')] hand in itertools.combinations(cards, 5): print (hand)
if need entire list in memory, try:
import itertools cards = [''.join(x) x in itertools.product('23456789tjqka', 'shdc')] big_list = list(itertools.combinations(cards, 5)) print len(big_list)
Comments
Post a Comment