user1778344
user1778344

Reputation: 13

Removing Duplicate Lists in Python

I'm doing a program that inputs a series of numbers, and takes 6 of them to make different combinations of lottery numbers. When I create the different combinations, I want to remove duplicates, so that each combination is only printed once. This is what I want to happen:

combo_list = [1 2 3 4 5 6 7]

And the output should be:

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 4 5 6 7
1 3 4 5 6 7
2 3 4 5 6 7

The code I'm using is:

   final = []
    for sublist in combo_list:
        if sublist not in final:
            final.append(sublist)
    for item in final:
        item = (sorted(item, key=int))
        print (' '.join(str(n) for n in item))

However, I get an output with many duplicates when I use the code:

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 6
1 2 3 4 6 7
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 6
1 2 3 5 6 7
1 2 3 4 5 7
1 2 3 5 6 7
1 2 3 4 5 6
1 2 3 4 6 7
1 2 3 4 5 6
1 2 3 5 6 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 4 5 7
1 2 3 5 6 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 3 4 5 6
.
.
.

Any ideas on what I have to change for each of the combinations to only print once?

Upvotes: 1

Views: 404

Answers (5)

cmchao
cmchao

Reputation: 86

 comb_list= range(1, 8)
 rip_idx = 6
 for idx in range(len(comb_list)):
    final = comb_list[0:rip_idx -idx ] + comb_list[rip_idx- idx + 1: ]
    print final

A simple way to achieve it solve without any library

Upvotes: 0

rh0dium
rh0dium

Reputation: 7032

Based on your desire to simply remove the duplicates.. Use python set

   final = []
    for sublist in combo_list:
        if sublist not in final:
            final.append(sublist)
    for item in set(tuple(x) for x in final):
        item = (sorted(item, key=int))
        print (' '.join(str(n) for n in item))

But FWIW - why re-create the wheel use the @Óscar López answer ;)

HTH

Upvotes: 0

dawg
dawg

Reputation: 103744

You can just take the source to itertools.combinations then:

def lotto(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

print list(lotto([1, 2, 3, 4, 5, 6, 7], 6))   

Upvotes: 0

Óscar López
Óscar López

Reputation: 235984

Use itertools.combinations() for this:

import itertools as it
ans = it.combinations([1, 2, 3, 4, 5, 6, 7], 6)

The result is as it should be:

list(ans)

=> [(1, 2, 3, 4, 5, 6), (1, 2, 3, 4, 5, 7),
    (1, 2, 3, 4, 6, 7), (1, 2, 3, 5, 6, 7),
    (1, 2, 4, 5, 6, 7), (1, 3, 4, 5, 6, 7),
    (2, 3, 4, 5, 6, 7)]

If you need to print the numbers afterwards it's easy:

for r in ans:
    print ' '.join(str(s) for s in r) 

=> 1 2 3 4 5 6
   1 2 3 4 5 7
   1 2 3 4 6 7
   1 2 3 5 6 7
   1 2 4 5 6 7
   1 3 4 5 6 7
   2 3 4 5 6 7

Upvotes: 5

user2423516
user2423516

Reputation:

If I understand your question, go ahead and try using the combinations function from the itertools module. In your case, you'll get:

>>> import itertools
>>> list(itertools.combinations([1,2,3,4,5,6,7],6)
[(1, 2, 3, 4, 5, 6)
(1, 2, 3, 4, 5, 7)
(1, 2, 3, 4, 6, 7)
(1, 2, 3, 5, 6, 7)
(1, 2, 4, 5, 6, 7)
(1, 3, 4, 5, 6, 7)
(2, 3, 4, 5, 6, 7)] 

which is what I think you want.

Remember that the output of the combinations function is a generator.

Upvotes: 0

Related Questions