我发现很多线程都在删除数组中的重复项,但是对于我的特定用例没有。我有一个二维列表,我需要删除重复,但我必须保持原始序列
mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
我需要简单地删除重复项而不重新安排,所以..
newlist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
感谢任何帮助
答案 0 :(得分:5)
使用set
跟踪所看到的内容:
>>> 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 not in seen:
... newlist.append(item)
... seen.add(t)
...
>>> newlist
[['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
注意强>
您需要将列表转换为元组(列表不可清除);无法添加要设置的列表。
>>> 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]))
>>>
答案 1 :(得分:2)
mylist = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
result = []
for x in mylist:
if x not in result:
result.append(x)
print result
[['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
答案 2 :(得分:0)
将numpy
导入为np
:
myList = [['Installation', '64%'], ['C2', '14%'], ['NA', '14%'], ['C2', '14%'], ['NA', '14%'], ['na', '7%']]
mylist=np.unique(myList,axis=0)
print (mylist)