标签:
生成随机验证码实例
import random a=[] c =‘‘ for i in range(6): if i == random.randrange(1,4): a.append(str(random.randint(1,9))) else: temp = random.randint(65,90) a.append(chr(temp)) print a b="".join(a) print b for i in a: c += str(i) print c
输出为
[‘D‘, ‘J‘, ‘A‘, ‘5‘, ‘B‘, ‘P‘] DJA5BP DJA5BP
———————知识延伸
1.用到了chr(i),中文说明:返回整数i对应的ASCII字符。与ord()作用相反。
2.字符拼接方式对比。下面是对比消耗时间的例子:
import cStringIO import StringIO import time values = [] items = [‘1,2,31,2,31,2,31,2,3‘] * 10000000 #print items st = float(time.time()) for i in items: values.append(i) news = ‘‘.join(values) print len(news) print ‘list append cost time:%s‘%(float(time.time()) - st) strio = cStringIO.StringIO() st = float(time.time()) for j in items: strio.write(j) print len(strio.getvalue()) print ‘cStringIO cost time:%s‘%(float(time.time()) - st) strio = StringIO.StringIO() st = float(time.time()) for j in items: strio.write(j) print len(strio.getvalue()) print ‘StringIO cost time:%s‘%(float(time.time()) - st)
标签:
原文地址:http://www.cnblogs.com/tudo/p/4904973.html