我有一个二维列表,我想删除重复项,但仍然通过使用列表理解将列表保持在列表中。例如,这是我的下面列表
Df = [[2,4,6,6,7],[3,4,5,7,9,3,5],[2,4,6,8]]
我希望我的结果成为
Df2 = [[2,4,6,7],[3,4,5,7,9,],[2,4,6,8]]
答案 0 :(得分:3)
如果订单不重要,应该这样做
Df2 = [list(set(item)) for item in Df]
如果顺序很重要,请尝试下面的代码
from collections import OrderedDict
Df2 = [list(OrderedDict.fromkeys(item)) for item in Df]
输入
Df = [[2,4,6,6,7],[3,4,5,7,9,3,5],[2,4,6,8]]
输出1
Df2 = [[2, 4, 6, 7], [3, 4, 5, 7, 9], [8, 2, 4, 6]]
Output2
Df2 =[[2, 4, 6, 7], [3, 4, 5, 7, 9], [2, 4, 6, 8]]