简体   繁体   中英

Removing Python List Items that Contain 2 of the Same Elements

I have a list myList, which contains items of the form

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

The first and second items are equal and so are the third and fourth, although their first and second elements are swapped. I would like to keep only one of each so that the final list looks like this:

a,b,3

c,d,1

e,f,4

Use sets and frozensets to remove equal elements:

>>> mySet = [frozenset(x) for x in myList]
>>> [tuple(x) for x in set(mySet)]
[('a', 3, 'b'), (4, 'e', 'f'), (1, 'c', 'd')]

the result can then be sorted however you'd like.

Take each tuple in myList, convert it to a list and apply sorted(). This results in a list filled with sorted inner lists which would look like.

myList = [('a','b',3), ('b','a',3), ('c','d',1), ('d','c',1), ('e','f',4)]
sorted_inner_list = [sorted(list(element)) for element in myList]
output = list(set(map(tuple,sorted_inner_list)))

You can use this to maintain the order of your tuples inside list and eliminate the duplicates by using set

>>> myList = [('a','b',3), ('b','a',3), ('c','d',1), ('d','c',1), ('e','f',4)]
>>> _ = lambda item: ([str,int].index(type(item)), item)
>>> sorted(set([tuple(sorted(i, key = _)) for i in myList]), key=lambda x: x[0])

Output:

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

If yo want to keep order of tuple, and always keep first tuple when there are duplicates , you can do :

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

If you prefer not to use another variable :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM