-3

my current output [1,2,3,4,5,6] [2,1,3,4,5,6] [3,2,1,4,5,6] ... [6,5,4,3,2,1] all numbers are same but i want like this output only one [1,2,3,4,5,6] how can i do this

import    os
import    sys

numbers=[1,2,3,4,5,6]
x=[]
for i1 in numbers:
    for i2 in numbers:
        for i3 in numbers:
            for i4 in numbers:
                for i5 in numbers:
                    for i6 in numbers:
                        if i1 not in (i2,i3,i4,i5,i6) and i2 not in (i1,i3,i4,i5,i6) and i3 not in (i1,i2,i4,i5,i6) and i4 not in (i1,i2,i3,i5,i6) and i5 not in (i1,i2,i3,i4,i6) and i6 not in (i1,i2,i3,i4,i5):
                            x.append([i1,i2,i3,i4,i5,i6])

i=0
while i < len(x):
    print x[i]
    i=i+1

3 Answers3

0

This is a rather unclear question, but to remove duplicates from a list:

nums = [1,2,3,3,4,5,6]
x = []
for i in nums:
    if i not in x:
        x.append(i)

[print(i) for i in x] # output -> 1 2 3 4 5 6

And to only get uniques from several lists:

nums = [1,2,3,3,4,5,6]
nums2 = [2,1,3,4,4,5,7,8]

lists = nums + nums2

x = []
for i in lists:
    if i not in x:
        x.append(i)

[print(i) for i in x] # output -> 1 2 3 4 5 6 7 8

Edit and answer

To get only unique sets of numbers:

nums1 = [1,2,3,4,5,6]
nums2 = [3,2,1,6,5,4]
nums3 = [4,3,2,5,1,6]
nums4 = [2,3,4,5,6,7]

s1 = set(nums1)
s2 = set(nums2)
s3 = set(nums3)
s4 = set(nums4)

sets = [s1, s2, s3, s4]
uniq = []
for s in sets:
    if s not in uniq:
        uniq.append(s)

for u in uniq:
    [print(i, end=' ') for i in u]
    print() # output -> {1,2,3,4,5,6} and {2,3,4,5,6,7}

Using sets is not required, but will ensure you have no duplicates inside the original lists and things will be easier in other ways as well.

Felix
  • 2,548
  • 19
  • 48
  • 2
    It might be better to use [`set`](https://docs.python.org/3/tutorial/datastructures.html#sets). – internet_user Jan 14 '18 at 12:57
  • 1
    @internet_user It's convenient, yes. But it seems quite clear OP doesn't have a lot of experience. So it's worth stating how to manually check for duplicates. – Felix Jan 14 '18 at 13:02
  • @AltayKarakalpaklı I edited the question for you and included an answer above according to your answer that was deleted. Please try to be precise in your questions in the future. – Felix Jan 14 '18 at 13:59
0

set() it!

Just use set() on the list you want to remove duplicates from. Then list() it to cast it to a list.

Adi219
  • 4,712
  • 2
  • 20
  • 43
0

You can use list comprehensions in python to achieve this.

Suppose you have multiple lists, and want to put all of the unique elements from these lists into one array, you can code it this way.

number_list_1 = [1,2,3,4,5]
number_list_2 = [4,3,2,1,6]
number_list_3 = [2,4,1,3,7]
no_dupes = []

[no_dupes.append(item) for item in number_list_1 if item not in no_dupes]
[no_dupes.append(item) for item in number_list_2 if item not in no_dupes]
[no_dupes.append(item) for item in number_list_3 if item not in no_dupes]

print str(no_dupes)

This would return: [1, 2, 3, 4, 5, 6, 7]

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • no i'am saying that we have three arrays 1,2,3,4; 4,3,2,1 and 2,4,1,3 and these 3 arrays contain same numbers how can i show only one unique array – Altay Karakalpaklı Jan 14 '18 at 13:31
  • If all the three arrays are going to always have the same numbers, they why not just sort one of them? `array.sort()` – grizzthedj Jan 14 '18 at 13:37
  • Well that is kind of up to you to figure out. It is still very unclear what you are trying to do, or what problem you are trying to solve. I only mentioned `array.sort()` because you said all lists will contain the same numbers, but in different order. So why even process them at all, is what I was thinking. – grizzthedj Jan 14 '18 at 14:06