1

I have a python script that prints out lists of numbers that are perfect cubes and add to 1978. Here is the output:

    `[1, 27, 729, 512, 8, 512, 1, 8]
     [1, 27, 729, 512, 8, 512, 8, 1]
     [1, 27, 729, 512, 512, 1, 8, 8]
     [1, 27, 729, 512, 512, 8, 1, 8]
     [1, 27, 729, 512, 512, 8, 8, 1]
     [1, 64, 8, 64, 125, 512, 512, 512]
     [1, 64, 8, 64, 512, 125, 512, 512]
     [1, 64, 8, 64, 512, 512, 125, 512]
     [1, 64, 8, 64, 512, 512, 512, 125]
     [1, 64, 8, 125, 64, 512, 512, 512]`

I only want the results once, and not dupplacates, as I do not care what order the numbers are in, so out of this sample, I would only want 2 answers instead of 10. Is there a way to do this?

Edit: also, the results take about a day to write out because there are so many, so I need something that can actively filter, so it goes faster

Luke Vanzweden
  • 466
  • 3
  • 15

2 Answers2

0

You can sort the results, convert them to tuples, and add them to a set to remove duplicates before printing. Something like this:

cube_lists = [ [1, 27, 729, 512, 8, 512, 1, 8],
     [1, 27, 729, 512, 8, 512, 8, 1],
     [1, 27, 729, 512, 512, 1, 8, 8],
     [1, 27, 729, 512, 512, 8, 1, 8],
     [1, 27, 729, 512, 512, 8, 8, 1],
     [1, 64, 8, 64, 125, 512, 512, 512],
     [1, 64, 8, 64, 512, 125, 512, 512],
     [1, 64, 8, 64, 512, 512, 125, 512],
     [1, 64, 8, 64, 512, 512, 512, 125],
     [1, 64, 8, 125, 64, 512, 512, 512]
    ]

result = set([tuple(sorted(r)) for r in cube_lists])
print([list(r) for r in result])

This prints:

[[1, 1, 8, 8, 27, 512, 512, 729], [1, 8, 64, 64, 125, 512, 512, 512]]
slider
  • 12,810
  • 1
  • 26
  • 42
  • This would work, howevr the results take about a day to print out, so I want something that can filter actively. – Luke Vanzweden Oct 06 '18 at 01:39
  • You can maintain the `result` set and as new values arrive, you can check whether their sorted tuple version is already in the `result` set (like `if sorted(tuple(new_list)) in result`). If it is, don't print, otherwise print and add the new list to `result`. – slider Oct 06 '18 at 02:03
0

One way to do it is to add to a big list first and then convert the list to a set, as you don't care about the order and don't want duplicates. When you are done, you can print out the list.

list_with_duplicates = [1, 27, 729, 512, 8, 512, 1, 8, 1, 27, 729, 512, 8, 512, 8, 1, 1, 27, 729, 512, 512, 1, 8, 8, 1, 27, 729, 512, 512, 8, 1, 8, 1, 27, 729, 512, 512, 8, 8, 1, 1, 64, 8, 64, 125, 512, 512, 512, 1, 64, 8, 64, 512, 125, 512, 512, 1, 64, 8, 64, 512, 512, 125, 512, 1, 64, 8, 64, 512, 512, 512, 125, 1, 64, 8, 125, 64, 512, 512, 512]
list_without_duplicates = set(list_with_duplicates)
print(list_without_duplicates) # Will print {512, 1, 64, 8, 729, 27, 125}` 

If you need to keep adding things to the set, you can use the .add() function to a set.

list_without_duplicates.add(1) 
print(list_without_duplicates) #Will print (same set) {512, 1, 64, 8, 729, 27, 125}
list_without_duplicates.add(2) 
print(list_without_duplicates) #Will print (new set) {512, 1, 64, 2, 8, 729, 27, 125}
Awaa
  • 178
  • 13