在python中使用HUGE列表

sco*_*ott 8 python poker python-itertools

我怎样才能管理一个包含1亿多字符串的庞大列表?我怎样才能开始使用如此庞大的列表?

示例大列表:

cards = [
            "2s","3s","4s","5s","6s","7s","8s","9s","10s","Js","Qs","Ks","As"
            "2h","3h","4h","5h","6h","7h","8h","9h","10h","Jh","Qh","Kh","Ah"
            "2d","3d","4d","5d","6d","7d","8d","9d","10d","Jd","Qd","Kd","Ad"
            "2c","3c","4c","5c","6c","7c","8c","9c","10c","Jc","Qc","Kc","Ac"
           ]

from itertools import combinations

cardsInHand = 7
hands = list(combinations(cards,  cardsInHand))

print str(len(hands)) + " hand combinations in texas holdem poker"
Run Code Online (Sandbox Code Playgroud)

Dav*_*ver 8

有很多很多的记忆.Python列表和字符串实际上相当有效,所以如果你有内存,它应该不是问题.

也就是说,如果您要存储的是专门的扑克手,那么您绝对可以提出更紧凑的表示方式.例如,您可以使用一个字节对每张卡进行编码,这意味着您只需要一个64位的int来存储整个手牌.然后,您可以将它们存储在NumPy数组中,这将比Python列表更有效.

例如:

>>> cards_to_bytes = dict((card, num) for (num, card) in enumerate(cards))
>>> import numpy as np
>>> hands = np.zeros(133784560, dtype='7int8') # 133784560 == 52c7
>>> for num, hand in enumerate(itertools.combinations(cards, 7)):
...     hands[num] = [cards_to_bytes[card] for card in hand]
Run Code Online (Sandbox Code Playgroud)

并加快了最后一行: hands[num] = map(cards_to_bytes.__getitem__, hand)

这只需要7*133784560 =〜1gb的内存......如果你将四张卡打包到每个字节中,这可能会被削减(我不知道这样做的语法在我的头顶......)


Jun*_*uxx 6

如果您只想循环遍历所有可能的手来计算它们或找到具有特定属性的手,则无需将它们全部存储在内存中.

您可以只使用迭代器而不是转换为列表:

from itertools import combinations

cardsInHand = 7
hands = combinations(cards,  cardsInHand)

n = 0
for h in hands:
    n += 1
    # or do some other stuff here

print n, "hand combinations in texas holdem poker."
Run Code Online (Sandbox Code Playgroud)

85900584德州扑克手牌组合.