现在我们的代码在set中存储了3,000,000个id,格式为string,我尝试使用list comprehension将其转换为int。但它耗时5秒,如何降低将这些3,000,000个ID转换为int的成本?
这是我的测试代码:
import random
import time
a = set()
for i in xrange(3088767):
a.add(str(random.randint(10005907, 100000000)))
start = time.time()
ld = [int(i) for i in a]
end = time.time()
print end-start
结果是:
$python -V
Python 2.6.5
$python ld.py
5.53777289391