从嵌套列表中删除重复项

时间:2017-08-26 16:58:49

标签: python list nested-lists

我有一个清单

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]

我希望将我的列表设为

A = [['1'],['1','2'],['1','2','3'],['3']]

即我想删除列表中元素中的重复元素。

4 个答案:

答案 0 :(得分:1)

单行(如果顺序无关紧要):

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
A = [list(set(a)) for a in A]
print(A) # => [['1'], ['2', '1'], ['3', '2', '1'], ['3']]

单行(如果订单很重要):

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
A = [sorted(set(a), key=a.index) for a in A]
print(A) # => [['1'], ['1', '2'], ['1', '2', '3'], ['3']]

答案 1 :(得分:1)

功能版本,functools

>>> import functools
>>> A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
>>> print ([functools.reduce(lambda result,x:result if x in result else result+[x], xs, []) for xs in A])
[['1'], ['1', '2'], ['1', '2', '3'], ['3']]

只有当列表中不存在该元素时,lambda函数才会向result列表添加元素。不是很有效,但保持元素的顺序。

另请注意,使用Python 2,您不需要导入functoolsreduce是内置函数。

答案 2 :(得分:0)

您可以使用OrderedDictfrom collections import OrderedDict优先)map,如下所示:

def foo(lst):
    return OrderedDict.fromkeys(lst, None).keys()

lst = [['1'], ['1', '2'], ['1', '2', '3', '1', '2'], ['3', '3', '3']]    
out = list(map(foo, lst))
print(out)

输出:

[['1'], ['1', '2'], ['1', '2', '3'], ['3']]

如果订单无关紧要,请转换为set并返回:

out = [list(x) for x in map(set, lst)]
print(out)

输出:

[['1'], ['1', '2'], ['1', '3', '2'], ['3']]

答案 3 :(得分:0)

您可以使用生成器:

def remove_dups(l):
   for a in l:
      new_l = []
      for b in a:
          if b not in new_l:
             new_l.append(b)
      yield new_l

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
print(list(remove_dups(A)))

输出:

[['1'], ['1', '2'], ['1', '2', '3'], ['3']]