Last active
January 4, 2016 19:19
-
-
Save gregroberts/8666134 to your computer and use it in GitHub Desktop.
For an ordered list, remove permutations and non-unique elements & preserve order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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