Python根据特定键从嵌套列表中删除重复项和原始文件

时间:2018-06-15 08:22:41

标签: python list duplicates nested-lists

我试图删除所有副本&原始来自基于特定列的嵌套列表。

实施例

list = [['abc',3232,'demo text'],['def',9834,'another text'],['abc',0988,'another another text'],['poi',1234,'text']]

键列是第一列(abc,def,abc),基于此我想删除与原始值具有相同值的任何项目(加上原始项目)。

所以新列表应该包含:

newlist = [['def',9834,'another text'],['poi',1234,'text']]

我发现了很多类似的主题,但没有找到嵌套列表...... 有什么帮助吗?

4 个答案:

答案 0 :(得分:2)

您可以构建一个键列表

keys = [x[0] for x in list]

并仅选择那些密钥恰好出现一次的记录

newlist = [x for x in list if keys.count(x[0]) == 1]

答案 1 :(得分:1)

使用列表理解。

<强>演示:

l = [['abc',3232,'demo text'],['def',9834,'another text'],['abc', 988,'another another text'],['poi',1234,'text']]
checkVal = [i[0] for i in l]
print( [i for i in l if not checkVal.count(i[0]) > 1 ] )

<强>输出:

[['def', 9834, 'another text'], ['poi', 1234, 'text']]

答案 2 :(得分:1)

collections.defaultdict用于O(n)解决方案:

L = [['abc',3232,'demo text'],
     ['def',9834,'another text'],
     ['abc',988,'another another text'],
     ['poi',1234,'text']]

from collections import defaultdict

d = defaultdict(list)

for key, num, txt in L:
    d[key].append([num, txt])

res = [[k, *v[0]] for k, v in d.items() if len(v) == 1]

print(res)

[['def', 9834, 'another text'],
 ['poi', 1234, 'text']]

答案 3 :(得分:1)

使用collections.Counter

from collections import Counter

lst = [['abc',3232,'demo text'],['def',9834,'another text'],['abc',988,'another another text'],['poi',1234,'text']]

d = dict(Counter(x[0] for x in lst))
print([x for x in lst if d[x[0]] == 1])

# [['def', 9834, 'another text'], 
#  ['poi', 1234, 'text']]

另请注意,您不应将列表命名为list,因为它会影响内置list