python学习笔记6--双色球需求实现
# 5,随机产生5条双色球号码 # blue 存蓝色的求 01,02 # red 存红色的求 17,16,03 # date存生成的时间,精确达到秒 #处理 import random,datetime import pymysql def add_zero(num): if len(num)==1: num='0'+num return num def tickets(num:int): all_nums=[]#存放所有的结果 all_blues=list(map(add_zero,[str(x) for x in range(1,17)])) all_reds = list(map(add_zero, [str(x) for x in range(1, 34)])) for i in range(num): new_all_reds=all_reds[:] #为了不让删空 tmp_reds=[] for i in range(6): choice=random.choice(new_all_reds) tmp_reds.append(choice) new_all_reds.remove(choice) blue=random.choice(all_blues) red=','.join(tmp_reds) all_nums.append([red,blue,datetime.datetime.now().strftime('%Y-%d-%m %H:%M:%S')]) return all_nums def insert_db(list): conn=pymysql.connect(host='192.168.1.125',user='root', passwd='123456', port=3306,db='python', charset='utf8') cur=conn.cursor() cur.executemany('insert into seq(red,blue,date)values(%s,%s,%s);',list) conn.commit() cur.close() conn.close() res=tickets(10) insert_db(res)