Just wanted to add a potential second solution if anyone has a use case where "first come, first serve" might matter.
For example, say we take three lists and merge them into a list of tuples:
# Make some lists (must be same size)
a = [1,1,1,2,8,6,1]
b = [2,4,6,1,4,21,69]
c = [2,8,21,2,1,1,8]
# Lists to list of tuples
arr = []
for i in range(len(a)):
new_row = (a[i],b[i],c[i])
arr.append(new_row)
Such that our original array looks like:
(1, 2, 2)
(1, 4, 8)
(1, 6, 21)
(2, 1, 2)
(8, 4, 1)
(6, 21, 1)
(1, 69, 8)
In our case, we want to remove items like (2,1,2) and (8,4,1) as they're equivalent to (1,2,2) and (1,4,8) respectively.
To do this, we can use a new empty list called filtered
or something, and itertools.permutations()
on each tuple in the original array.
First, we check if any permutation of each item is present in the filtered
list.
If not, we add. If it is, we skip the duplicate.
filtered = []
for i in range(len(arr)):
it = itertools.permutations(arr[i])
perms = []
for p in it:
perms.append(p)
check = any(item in perms for item in filtered)
if not check:
filtered.append(arr[i])
Now if we iterate over filtered
and print, we see our truncated list of tuples:
(1, 2, 2)
(1, 4, 8)
(1, 6, 21)
(1, 69, 8)
Note that we're left with the first instance of each tuple of numbers, and not working via a set
guarantees the same order of elements when iterating over the filtered list.
Only thing I'm not 100% on is the time/space complexity of doing it this way -- if anyone has feedback I'd love to hear about it.
('a','b') != ('b','a')
or more specifically,hash(('a','b')) != hash(('b','a'))
. Do an initial list comprehension that sorts each tuple then convert to a set – Cheese('a','b')
and('b','a')
to not be equal.... – Cheese