把range改为xrange。xrange就是一个迭代方式的,而range会一次性在内存中生成列表,所以会慢。好象从2.6还是多少版本range就是使用xrange替代了。
--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: http://uliwebproject.appspot.com
My Blog: (new)http://http://hi.baidu.com/limodou
(old)http://www.donews.net/limodou
只有一个数试试,好象不行,如:
>>> range(10000000000)
Traceback (most recent call last):
File "<input>", line 1, in <module>
OverflowError: range() result has too many items
不知道有没有,没用过很大的。
我这里是报错了,我用的是32位的。我是随便试的,好象是不能太大的数。看到itertools模块有一个count函数:
count( [n])
Make an iterator that returns consecutive integers starting with n. If
not specified n defaults to zero. Does not currently support python
long integers. Often used as an argument to imap() to generate
consecutive data points. Also, used with izip() to add sequence
numbers. Equivalent to:
def count(n=0):
while True:
yield n
n += 1
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.
可以考虑用它,或自已照着写一个。
大哥,这可是100亿个整数啊。就算是4字节一个整数,那也是40个G的数据量呢。
--
Best Regards,
Leo Jay
很大吗?python直接支持大数运算的:
>>> 10000000000 * 10000000000
100000000000000000000L
不过对于range()或xrange()可能是有限制的。
我知道python直接大数运算,但range(n)会生成一个长度为n的list,这个list如果有100亿个整数,一般的机器哪放得下啊。
因为我使用了xrange也是不行:
>>> for x in xrange(10000000000):
... if x > 10:
... break
... print x
...
Traceback (most recent call last):
File "<input>", line 1, in <module>
OverflowError: long int too large to convert to int
2.6里可以:
>>> for i in xrange(2*100):
... if i > 10: break
... print i
...
0
1
2
3
4
5
6
7
8
9
10
靠,丢人了。。。
2.6里也不行。。。
3.0 的 range 可以,
2.5,2.6 的 xrange 和 range 都是 long 不可以
嗯,3.0 里的 int 就是超长整数了
$ python3.0
Python 3.0b3 (r30b3:65927, Sep 14 2008, 01:05:23)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(2**100)
range(0, 1267650600228229401496703205376)
>>>
$ python2.5
Python 2.5.2 (r252:60911, May 7 2008, 15:19:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> xrange(2**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
>>> range(2**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: range() result has too many items
>>>
$ python2.6
Python 2.6 (r26:66714, Oct 21 2008, 21:50:32)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> xrange(2**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
>>> range(2**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>