BubbleMonster BubbleMonster - 1 year ago 149
Python Question

Python - all lottery combinations - no duplicates, in order

I've wrote a script to print out lottery combinations. What i'm aiming for is this: There are 6 numbers in the lottery between 1-49 meaning there are 13,983,816 combinations. I want to print out all combinations IN ORDER whilst making sure there are NO duplicates.

Here is my code so far:

import random
numbers = []
for i in range(2):
for j in range(6):
for k in range(j):
while numbers[j]==numbers[k]:
print sorted(numbers)
numbers = []
f = open('combinations.txt', 'w')

The problems are:

The output in the terminal is:

[18, 20, 27, 32, 44, 48]
[5, 7, 10, 13, 33, 45]

I want to start with
and end up at
. So I need to order the results.

Also, I've tried converting the list to a string so I can put the results in a big text file, but I'm just printing
to the text file at the moment.

Answer Source

You were clearing your list then writing to the file.

from itertools import combinations
f = open('combinations.txt', 'w')
for comb in combinations(range(1,50), 6):

But make sure you have at least 350 Megabytes of disk space free! and some time to spare.

(348168480 bytes I checked with:

>>> s = 0
>>> for comb in combinations(range(1,50), 6):
...    s += len(repr(comb))+2
>>> s


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download