2017-07-25 5 views
0

私はいくつかの計算で使用する5つのカードポーカーハンドのリストを作成しようとしています(遅いかもしれませんが、やや速いことが好ましい)。今リストを取得するために、私は次のコードを書いた:可能なすべての5カードポーカーのリストを作る

import itertools 

# All 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 all non-trivial cartesian products 
for 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 all elements and delete duplicates 
for x in hands: 
    x.sort() 
hands = [tuple(x) for x in hands] 
hands = list(set(hands)) 
# Convert hands back to a list 
hands = [list(x) for x in hands] 

# Verify result 
print(str(len(hands))) 

をしかし、それは(RAMの11以上のライブを)完了しています前に、これはメモリ不足します。私はそのリストを使用しようとしているので、2つの手をお互いにしようとすると、可能なすべての手のセットに対して徹底的にテストすることができます。

このコードを改善する方法を知っている人はいますか?

+0

]あなたの仕事を達成する。通常は悪い考えです。 – user2357112

+0

お互いに両手を比較するために、すべての可能な手のリストは必要ありません。 – user2357112

答えて

1

まず、作成しようとしている機能が既に存在します:itertools.combinations。次に、コードを構造化して、それらをすべて同時にメモリに入れることなく、すべての可能なハンドを繰り返し処理できるようにしてください。ここで

は重複した手を外して、すべての可能な手をプリントする短いプログラムではありませんが、決してすべての可能な手のインメモリリスト作成:あなたが実際にあるように、リスト全体を必要とする場合

import itertools 
cards = [''.join(x) for x in itertools.product('23456789TJQKA', 'shdc')] 

for hand in itertools.combinations(cards, 5): 
    print (hand) 

をメモリ:試してみてください:

import itertools 
cards = [''.join(x) for x in itertools.product('23456789TJQKA', 'shdc')] 
big_list = list(itertools.combinations(cards, 5)) 
print len(big_list) 
+0

また、カードに文字列を使用しないでメモリを節約してください。整数を使用します。 –

+0

真。その点を上げるだけで水が泥だらけなのかどうか判断できませんでした。 –

1

あなたは〜52^5 =〜3億5千万の手を作り、それを並べ替えようとしています。それは多くの記憶をとるだろう。各手の各要素が一意であることを確認するロジックを修正する必要があります。あなたが現在持っているものは、それらがすべて同じであるものだけを削除します。

c1, c2, c3, c4, c5 = "2s", "2s", "2s", "2s", "3s" 
print(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) 
>>>True 

あなたは論理積を持つすべてのORSを置き換えることができるのいずれか、または要素のセットが重複して手を排除する要素自体に等しい場合、あなただけのテストができます。

c1, c2, c3, c4, c5 = "2s", "2s", "2s", "2s", "3s" 
print(list(set([c1,c2,c3,c4,c5])).sort() == [c1,c2,c3,c4,c5].sort()) 
>>>False 
c1, c2, c3, c4, c5 = "2s", "3s", "4s", "5s", "6s" 
print(list(set([c1,c2,c3,c4,c5])).sort() == [c1,c2,c3,c4,c5].sort()) 
>>>True 

これは、52までの手の数を5 =〜260万に削減します。これははるかに管理しやすくなります。

2

実際にPythonにはいくつかの電池が付属しています。

Here is the functionそれはあなたのためにそれを行います。あなた自身が「すべての可能な[もの]のリストを作る」考えを見つけるたび、あなたは第1のメモリに、それはすべての可能な[もの]を詰め込むためにも、少しでも妥当なのかどうかを検討したり、すべての可能な[事を越える必要がある

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 = itertools.combinations(cards, 5) 

for hand in hands: 
    print(hand) 
関連する問題