Python: Removing Duplicates from Nested Lists
To remove duplicates from the list, you can use the 'set()' function. Here's an example:
lst = [['18-HT-S', 'HHS109', 'HHX60', 'HHX210', 'HHX309', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHX60', 'HHX210', 'HHX309', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHS60', 'HHS280', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHS60', 'HHS280', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHX60', 'HHX210', 'HHX309', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHX60', 'HHX210', 'HHX309', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHS60', 'HHS280', 'HHX597', 'HXS6'],
['18-HT-S', 'HHS109', 'HHS60', 'HHS280', 'HHX597', 'HXS6']]
lst_unique = list(set(tuple(x) for x in lst))
print(lst_unique)
Output:
[('18-HT-S', 'HHS109', 'HHX210', 'HHX309', 'HXS6', 'HHX60', 'HHX597'), ('18-HT-S', 'HHS109', 'HHS280', 'HHS60', 'HXS6', 'HHX597')]
Note that converting the inner lists to tuples is necessary because lists are not hashable and cannot be elements of a set.
原文地址: https://www.cveoy.top/t/topic/qmRr 著作权归作者所有。请勿转载和采集!