简体   繁体   English

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

[英]Python Remove duplicates and original from nested list based on specific key

I m trying to delete all duplicates & original from a nested list based on specific column. 我正在尝试根据特定的列从嵌套列表中删除所有重复项和原始项。

Example

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

The key column is the first (abc, def, abc) and based on this I want to remove any item (plus the original) which has the same value with the original. 关键列是第一列(abc,def,abc),基于此,我想删除与原始值相同的任何项目(加上原始值)。

So the new list should contain: 因此,新列表应包含:

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

I found many similar topics but not for nested lists... Any help please? 我找到了许多类似的主题,但没有嵌套列表...有什么帮助吗?

You can construct a list of keys 您可以构造一个键列表

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

and select only those records for which the key occurs exactly once 并只选择键恰好出现一次的那些记录

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

Using a list comprehension. 使用列表理解。

Demo: 演示:

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 ] )

Output: 输出:

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

Using collections.defaultdict for an O(n) solution: 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']]

Use collections.Counter : 使用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']]

Also note that you shouldn't name your list as list as it shadows the built-in list . 另请注意,您不应将列表命名为list因为它会遮盖内置list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM