无法在 Python 3 中打印列表(范围(4**4**4))

unable to print list(range(4**4**4)) in Python 3

谁能帮我解释一下为什么我无法打印

>>> list(range(4**4**4)).

我收到一个错误 OverflowError:range() 结果有太多项目

根据官方文档:

https://docs.python.org/2/library/sys.html#sys.maxsize https://docs.python.org/3/library/sys.html#sys.maxsize

sys.maxsize The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

尝试使用此代码来检查您是否能够创建适合您尺寸的列表:

>>> import sys
>>> sys.maxsize > 4**4**4

并试试这个来验证 python 在你的情况下如何解决权力:

>>> print(2**2**3)
256
>>> print(2**(2**3))
256
>>> print((2**2)**3)
64
  • 如果列表中的元素多于有符号 long 可以容纳的元素,您将收到 OverflowError:

    • 在 32 位上 Python:2**31 - 1
    • 64 位 Python: 2**63 - 1
    • 即使是低于该值的值,您也可能会收到 MemoryError。

    比如你的号码很大:

    • 2**64 = 18446744073709551616
    • 4**4**4 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096