原题 | Storing a list in an int (https://iantayler.com/2020/12/07/storing-a-list-in-an-int)html
做者 | Computer Witpython
译者 | 豌豆花下猫(“Python猫”公众号做者)算法
声明 | 本翻译已获得原做者受权。为便于阅读,内容略有改动。数据库
与 C、Rust 和 Go 不一样,Python 默认的int
具备任意大小。[注1] 、[注2] 编程
这意味着,一个整数能够存储无限大的值,只要内存足够。数组
例如,你能够打开 Python3 并运行如下命令:数据结构
>>> import math >>> math.factorial(2020) [number omitted] # Python猫注:此处求2020的阶乘,结果是一长串数字,因此省略 >>> math.log2(math.factorial(2020)) 19272.453841606068 >>> type(math.factorial(2020)) <class 'int'>
也就是说,在 Python 中,日常使用的 int 能够轻松地保存一个占用 19273 比特的 C 类型固定大小无符号 int 类型的值(C-style fixed-size unsigned int )。在 Python 这样的语言中,便利性高于速度和内存效率,这确实颇有用。app
这种无限的精度,也意味着咱们能够在单个 int 中存储任意数量的信息。只要编码正确,一整本书、一整个数据库、甚至任何东西,均可以被存入一个单独的 Python int 中。编程语言
(Python猫注:这有一篇文章 ,深度剖析了 Python 整型不会溢出的实现原理,可做关联阅读)ide
所以,咱们能够设想出一种 Python 的方言,它只有整型,须要用 int 表示其它全部的类型(字典、列表、等等)。咱们还有一些特殊的函数和方法,能够将 int 视为 list 、dict 等等。
这将会是一个有趣而好玩的练习,而这就是本文想要作的事。
有一个显而易见的实现方法:全部数据结构只是内存中的位数组(bit-arrays)。最坏的状况下,它是一组相关的位数组(例如,像链表或树中的每一个节点),而且它们的集合也只是位数组。位数组能够被解释为二进制数。因此咱们必然能这样作。但这有点无聊。
在本博文以及本系列的后续博文中,我将介绍一些用 int 来表示复杂数据结构的方法。它们不必定是最紧凑、最合理或最有效的,其共同的目标是找到这些数据结构的有趣的表示方式。[注3]
咱们要表示的第一个数据结构是 list。咱们将使用以逻辑学家 KurtGödel 命名的Gödel数。为了方便起见,咱们仅处理由无符号整数(即天然数)组成的列表。
哥德尔数的原理是令每一个大于 1 的天然数都用惟一的质数分解来表示。它依据的是算术的基本定理。
(Python猫注:质数分解,即 prime factorization,又译做质因数分解、素因子分解等,指的是把每一个数都写成用质数相乘的形式)
看一些例子:
一个数字能够经过其质因子(prime factors )的指数列表来惟一标识(直到其最高位的非零指数)。因此,咱们能够用 126 来表示列表[1, 2, 0, 1] 。列表中的第一个数字是 126 做质数分解后 2 的指数,第二个数是 3 的指数,依此类推。
再来几个例子:
若是列表末尾有 0 ,该怎么办呢?好吧,基于这样的编码,不会出现这种状况。
在咱们的质数分解中,指数为 0 的质数可能有无限个,所以咱们须要停在某个地方。[注4] 咱们选择在最后一个非零指数处中止。
当列表中包含较大的数字时,这种表示形式也会使用很是大的数字。那是由于列表中的数字表示的是指数,因此 int 的大小与它们成指数增加。例如,[50, 1000, 250] 须要使用大小为 2266 比特的数字表示。
另外一方面,相比于其它用 int 编码的列表,那些包含很是多小整数的长列表,尤为是大型稀疏列表(即大部分的值都为 0),则拥有很是紧凑的表示形式。
提醒一下,将 list 编码为 int,这不是很好的编程实践,仅仅是一个好玩的实验。
让咱们看一下 Python 的实现。这里有几点注意事项:
咱们要编写的第一个函数是一个迭代器,它将按顺序生成质数。它从头至尾都很关键。这里的实现是最简单可行的版本。
我可能很快会写一篇完整的关于生成质数的算法的文章,由于这是一个很酷的话题,自己也是一个古老的研究领域。最广为人知的算法是爱拉托逊斯筛法(Sieve of Erathosthenes ),但这只是冰山一角。[注6]
在这里,一个很是幼稚的实现就够了:
def primes(starting: int = 2): """Yield the primes in order. Args: starting: sets the minimum number to consider. Note: `starting` can be used to get all prime numbers _larger_ than some number. By default it doesn't skip any candidate primes. """ candidate_prime = starting while True: candidate_factor = 2 is_prime = True # We'll try all the numbers between 2 and # candidate_prime / 2. If any of them divide # our candidate_prime, then it's not a prime! while candidate_factor <= candidate_prime // 2: if candidate_prime % candidate_factor == 0: is_prime = False break candidate_factor += 1 if is_prime: yield candidate_prime candidate_prime += 1
def empty_list() -> int: """Create a new empty list.""" # 1 is the empty list. It isn't divisible by any prime. return 1
def iter_list(l: int): """Yields elements in the list, from first to last.""" # We go through each prime in order. The next value of # the list is equal to the number of times the list is # divisible by the prime. for p in primes(): # We decided we will have no trailing 0s, so when # the list is 1, it's over. if l <= 1: break # Count the number of divisions until the list is # not divisible by the prime number. num_divisions = 0 while l % p == 0: num_divisions += 1 l = l // p # could be / as well yield num_divisions
def access(l: int, i: int) -> int: """Return i-th element of l.""" # First we iterate over all primes until we get to the # ith prime. j = 0 for p in primes(): if j == i: ith_prime = p break j += 1 # Now we divide the list by the ith-prime until we # cant divide it no more. num_divisions = 0 while l % ith_prime == 0: num_divisions += 1 l = l // ith_prime return num_divisions
def append(l: int, elem: int) -> int: # The first step is finding the largest prime factor. # We look at all primes until l. # The next prime after the last prime factor is going # to be the base we need to use to append. # E.g. if the list if 18 -> 2**1 * 3**2 -> [1, 2] # then the largest prime factor is 3, and we will # multiply by the _next_ prime factor to some power to # append to the list. last_prime_factor = 1 # Just a placeholder for p in primes(): if p > l: break if l % p == 0: last_prime_factor = p # Now get the _next_ prime after the last in the list. for p in primes(starting=last_prime_factor + 1): next_prime = p break # Now finally we append an item by multiplying the list # by the next prime to the `elem` power. return l * next_prime ** elem
你能够打开一个 Python、iPython 或 bPython会话,并试试这些函数!
建议列表元素使用从 1 到 10 之间的数字。若是使用比较大的数字,则 append 和 access 可能会花费很长时间。
从某种程度上说,使用哥德尔数来表示列表并不实用,尽管能够经过优化质数生成及分解算法,来极大地扩大可用数值的范围。
In [16]: l = empty_list() In [17]: l = append(l, 2) In [18]: l = append(l, 5) In [19]: list(iter_list(l)) Out[19]: [2, 5] In [20]: access(l, 0) Out[20]: 2 In [21]: access(l, 1) Out[21]: 5 In [22]: l Out[22]: 972 # Python猫注:2^2*3^5=972
咱们看到了一种将天然数列表表示为 int 的方法。还有其它更实用的方法,这些方法依赖于将数字的二进制形式细分为大小不一的块。我相信你能够提出这样的建议。
我之后可能会写其它文章,介绍更好的用于生成和分解质数的算法,以及其它复杂数据结构的 int 表示形式。
int
, we can always write the length of the list as the exponent of 2 and start the actual list with the exponent of 3. This has some redundant information, though. The way to avoid redundant information is to store the number of final 0s in the list, instead of the entire length. We won’t be worrying about any of this, though.Python猫注: 以上是所有译文,但我最后还想补充一个有趣的内容。在《黑客与画家》中,保罗·格雷大师有一个惊人的预言,他认为在逻辑上不须要有整数类型,由于整数 n 能够用一个 n 元素的列表来表示。哈哈,这跟上文的脑洞刚好反过来了!想象一下,一个只有整数类型没有列表的编程语言,以及一个只有列表类型没有整数的编程语言,哪个更有可能在将来出现呢?