python彩票13取9胆组程序

是帮一个朋友做的彩票程序,也不知道有啥用,反正就是多个数字(>9)中随机取出9个,然后找出出现次数相同的5个数。具体需求如下:

1、导入一组数,最多是15个
2、从这组数中选出9个,所有可能的数组都列举出来
3、从每个数组中统计0~9的次数
4、如果有5个数出现次数相同,把这5个数列举出来,并加上出现次数;如果《5或》5,则不列举

 1 import itertools
 2 import csv
 3 
 4 l = []
 5 f = open('num9.txt')
 6 for n in f.readlines():
 7     n = n.strip()
 8     n = int(n)
 9     l.append(n)
10 
11 def qu(d2):
12     d3 = {}
13     for x, y in d2.items():
14         if y not in d3.values():
15             d3[x] = y
16     return d3
17 
18 def nine_num(num_list):
19     num_str = []
20     for n in num_list:
21         num_str.append(str(n))
22 
23     num_line = ''.join(num_str)
24 
25     d1 = {}
26     for n in num_line:
27         if n not in d1:
28             d1[n] = 0
29         d1[n] += 1
30 
31     d2 = {}
32     for a, b in d1.items():
33         if b not in d2:
34             d2[b] = ''
35         d2[b] += a
36 
37     d3 = qu(d2)
38 #    print d3
39     for n in d3:
40         if len(d3[n]) == 5:
41             return num_list, n, d3[n][::-1]
42 
43 all_list = list(itertools.combinations(l, 9))
44 
45 
46 norepeat = []
47 with open('num9.csv', 'wb') as csvfile:
48     spamwriter = csv.writer(csvfile, dialect = 'excel')
49     for x in all_list:
50         res = nine_num(x)
51         if res:
52             if res[2] not in norepeat:
53                 norepeat.append(res[2])
54                 spamwriter.writerow(res)

最后使用py2exe生成了exe程序,只需将多个数字一行一行的放入num9.txt,点击num9.exe,就会生成num9.csv文件。另外,最后的结果已经筛选了重复部分,生成的都是不重复的。

PS,本来使用了Tkinter做个简单界面,但最后老是提示memoryerror,最后只能做这种了。不过还好朋友喜欢。

posted on 2013-04-08 20:28  alexkh  阅读(772)  评论(0编辑  收藏  举报