Remove Duplicates In Two-dimensional Array While Maintaining Sequence
I have found a lot of threads on removing duplicates in arrays but none for my specific use-case. I have a two-dimensional list that I need to remove duplicates from however I must
Solution 1:
Using set
to keep track of seen items:
>>>mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]>>>seen = set()>>>newlist = []>>>for item in mylist:... t = tuple(item)...if t notin seen:... newlist.append(item)... seen.add(t)...>>>newlist
[['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
NOTE
You need to convert a list to tuple (list is not hashable); can't add a list to set.
>>>seen = set()>>>seen.add([1,2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>seen.add(tuple([1,2]))>>>
Solution 2:
mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
result= []
for x in mylist:
if x notinresult:
result.append(x)
print result
[['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
Solution 3:
import numpy
as np
:
myList = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
mylist=np.unique(myList,axis=0)
print (mylist)
Post a Comment for "Remove Duplicates In Two-dimensional Array While Maintaining Sequence"