Removing Python List Items that Contain 2 of the Same Elements -


i have list mylist, contains items of form

mylist = [('a','b',3), ('b','a',3), ('c','d',1), ('d','c',1), ('e','f',4)] 

the first , second items equal , third , fourth, although first , second elements swapped. keep 1 of each final list looks this:

a,b,3

c,d,1

e,f,4

if yo want keep order of tuple, , keep first tuple when there duplicates, can :

>>> sets = [ frozenset(x) x in mylist ] >>> filtered = [ mylist[i] in range(len(mylist)) if set(mylist[i]) not in sets[:i] ] >>> filtered [('a', 'b', 3), ('c', 'd', 1), ('e', 'f', 4)] 

if prefer not use variable :

filtered = [ mylist[i] in range(len(mylist))             if set(mylist[i]) not in [ frozenset(x) x in mylist ][:i] ] 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -