Skip to content

Instantly share code, notes, and snippets.

@gregroberts
Last active January 4, 2016 19:19
Show Gist options
  • Save gregroberts/8666134 to your computer and use it in GitHub Desktop.
Save gregroberts/8666134 to your computer and use it in GitHub Desktop.
For an ordered list, remove permutations and non-unique elements & preserve order
from itertools import permutations
def ret_unique(a):
'''returns a copy of the list with:
order preserved,
permutations removed,
repeat elements removed'''
for i,el in enumerate(a):
b = permutations(el,3)
for el1 in b:
if list(el1) in a[i+1:]:
del a[a.index(list(el1))]
if list(el1) in a[:i-1]:
del a[i]
return a
a=[['a','b','c'],['d','e','f'],['b','c','a'],['f','d','e'],['a','d','f'],['a','b','c'],['d','e','f'],['b','c','a'],['f','d','e'],['a','d','f']]
print ret_unique(a)
[['a', 'd', 'f'], ['a', 'b', 'c'], ['d', 'e', 'f']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment