使用python中的大列表

2024-06-08 05:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如何管理一个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"

Tags: 字符串示例列表asjs单子cardsks
3条回答

另一个不需要内存的选项是使用生成器,它允许您创建一个数据流来进行您喜欢的处理。例如。

打印手的总数:

sum (1 for x in combinations(cards, 7))

打印带有球杆王牌的手数:

sum (1 for x in combinations(cards, 7) if 'Ac' in x)

有很多很多很多的记忆。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]

把最后一行的速度提高一点:hands[num] = map(cards_to_bytes.__getitem__, hand)

这只需要7*133784560=~1gb的内存……如果你把四张卡放在每个字节中(我不知道这样做的语法…)

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

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

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."

85900584 hand combinations in texas holdem poker.

相关问题 更多 >

    热门问题