Python根据特定键从嵌套列表中删除重复项和原始项
我试图根据特定列从嵌套列表中删除所有重复项和原始项 范例Python根据特定键从嵌套列表中删除重复项和原始项,python,list,duplicates,nested-lists,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'],['p
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']]
我发现了许多类似的主题,但不适用于嵌套列表。。。
有什么帮助吗?请使用列表理解 演示:
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']]
输出:
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']]
使用列表理解 演示:
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']]
输出:
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']]
您可以构造一个键列表
keys = [x[0] for x in list]
并仅选择键只出现一次的记录
newlist = [x for x in list if keys.count(x[0]) == 1]
您可以构造一个键列表
keys = [x[0] for x in list]
并仅选择键只出现一次的记录
newlist = [x for x in list if keys.count(x[0]) == 1]
对O(n)解决方案使用
collections.defaultdict
:
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']]
对O(n)解决方案使用
collections.defaultdict
:
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']]
使用:
还要注意的是,您不应该将列表命名为list
,因为它会隐藏内置项。使用:
还要注意的是,您不应该将列表命名为
list
,因为它会隐藏内置项。到目前为止您尝试了什么?侧点。切勿在内置变量后命名变量,请使用L
或list\u
而不是list
。到目前为止您尝试了什么?侧重点。切勿在内置变量后命名变量,请使用L
或list\u
而不是list
。通过调用list.count
n次,此处的复杂性为O(n^2)。您可以使用collections.Counter
将此设置为O(n)。或者单独存储您的计数。好吧,OP没有说明任何关于列表大小的内容,所以我假设它不够大,不足以区分O(n)和O(n^2)。当然,使用计数器是更有效的方法,但我打算给出一个在大多数情况下都能很好地工作的快速而肮脏的解决方案。我的评论不是抱怨,只是一个可能引起读者兴趣的注释。没有冒犯;)刚才解释了我的意图。通过调用list.count
n次,这里的复杂性为O(n^2)。您可以使用collections.Counter
将此设置为O(n)。或者单独存储您的计数。好吧,OP没有说明任何关于列表大小的内容,所以我假设它不够大,不足以区分O(n)和O(n^2)。当然,使用计数器是更有效的方法,但我打算给出一个在大多数情况下都能很好地工作的快速而肮脏的解决方案。我的评论不是抱怨,只是一个可能引起读者兴趣的注释。没有冒犯;)刚才解释了我的意图。通过调用list.count
n次,这里的复杂性为O(n^2)。您可以使用collections.Counter
将此设置为O(n)。或者单独存储计数。通过调用list.count
n次,这里的复杂性为O(n^2)。您可以使用collections.Counter
将此设置为O(n)。或者单独存储您的计数。好的解决方案,这具有O(n)复杂性。但是我不认为如果d.keys()中的x[0]是必要的,
。@jpp oops!这是没有必要的。非常感谢。很好的解决方案,这有O(n)复杂性。但是我不认为如果d.keys()中的x[0]是必要的,
。@jpp oops!这是没有必要的。非常感谢。这个解决方案也很好。我通常选择Counter
而不是defaultdict
方式+1.这两种解决方案之间也很好。我通常选择Counter
而不是defaultdict
方式+1.