koo*_*gee 0 python random python-2.7
random.sample(range(2**31 - 1), random.randrage(1, 100))
Run Code Online (Sandbox Code Playgroud)
这导致:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
Run Code Online (Sandbox Code Playgroud)
我在64位的ubuntu 12.04和6GB RAM上运行python 2.7.3.
I thought 2**31 -1 was the normal upper limit for integers in a 32-bit computer. I'm still 1 below that and I'm getting a memory error?
您可能指C的int是语言中整数的限制,例如通常为4个字节.在Python 2.7中,整数没有限制,并在需要时自动提升为更大的类型,保持无限精度.你的问题不直接关系到这一点,你试图从创建每个号码的清单0来2**31-1,这将需要非常大量的内存,因此MemoryError.
您不需要创建此列表,只需使用xrangelazzily评估:
>>> random.sample(xrange(2**31 - 1), random.randrange(1, 100))
[248934843, 2102394624, 1637327281, 352636013, 2045080621, 1235828868, 951394286, 69276138, 2116665801, 312166638, 563309004, 546628308, 1890125579, 699765051, 1567057622, 656971854, 827087462, 1306546407, 1071615348, 601863879, 410052433, 1932949412, 1562548682, 1772850083, 1960078129, 742789541, 175226686, 1513744635, 512485864, 2074307325, 798261810, 1957338925, 1414849089, 1375678508, 1387543446, 1394012809, 951027358, 1169817473, 983989994, 1340435411, 823577680, 1078179464, 2051607389, 180372351, 552409811, 1830127563, 1007051104, 2112306498, 1936077584, 2133304389, 1853748484, 437846528, 636724713, 802267101, 1398481637, 15781631, 2139969958, 126055150, 1997111252, 1666405929, 1368202177, 1957578568, 997110500, 1195347356, 1595947705, 2138216956, 768776647, 128275761, 781344096, 941120866, 1195352770, 1244713418, 930603490, 2147048666, 2029878314, 2030267692, 1471665936, 1763093479, 218699995, 1140089127, 583812185, 1405032224, 851203055, 1454295991, 1105558477, 2118065369, 1864334655, 1792789380, 386976269, 1213322292, 663210178, 1402712466, 1564065247]
Run Code Online (Sandbox Code Playgroud)
xrange是不是一台发电机的-这是懒洋洋地评估一个序列对象.它支持几乎所有相同的方法,range但不需要将其数据存储在列表中.(在Python 3中,它简称为range).因此它有一个长度,random.sample可以立即访问它从中挑选k项目.
注意:关于2**31-1(在32位,sys.maxint对于您的特定系统)是最大的数字,从某种意义上说你是正确的,因为xrange上限有限(这并不意味着Python int有最大的大小)同时请注意Python 3正常修复此问题range.
>>> random.sample(xrange(2**31), random.randrange(1, 100))
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
random.sample(xrange(2**31), random.randrange(1, 100))
OverflowError: Python int too large to convert to C long
Run Code Online (Sandbox Code Playgroud)