[英]Removing dupes in list of lists in Python
基本上,我试图删除任何以相同值开头的列表。 例如,下面的两个以数字1开头:
a = [[1,2],[1,0],[2,4],[3,5]]
因为值1存在于两个列表的开头 - 我需要删除它们以便新列表变为:
b = [[2,4],[3,5]]
我怎样才能做到这一点?
我试过下面的,但输出是: [[1, 2], [2, 4], [3, 5]]
def unique_by_first_n(n, coll):
seen = set()
for item in coll:
compare = tuple(item[:n])
print compare # Keep only the first `n` elements in the set
if compare not in seen:
seen.add(compare)
yield item
a = [[1,2],[1,0],[2,4],[3,5]]
filtered_list = list(unique_by_first_n(1, a))
一个有效的解决方案是创建一个Counter
对象来保存第一个元素的出现,然后过滤主列表中的子列表:
from collections import Counter
counts = Counter(l[0] for l in a)
filtered = [l for l in a if counts[l[0]] == 1]
#[[2, 4], [3, 5]]
如果您乐意使用第三方库,可以使用Pandas:
import pandas as pd
a = [[1,2],[1,0],[2,4],[3,5]]
df = pd.DataFrame(a)
b = df.drop_duplicates(subset=[0], keep=False).values.tolist()
print(b)
[[2, 4], [3, 5]]
诀窍是keep=False
参数,在pd.DataFrame.drop_duplicates
的文档中pd.DataFrame.drop_duplicates
。
您可以使用collections.Counter
with list comprehension来获取第一个项目只出现一次的子列表:
from collections import Counter
c = Counter(n for n, _ in a)
b = [[x, y] for x, y in a if c[x] == 1]
解决方案1
a = [[1,2],[1,0],[2,4],[3,5]]
b = []
for item in a:
i = 0
if item[0] == a[i][0]:
i =+ 1
continue
else:
b.append(item)
i += 1
解决方案2
a = [[1,2],[1,0],[2,4],[3,5]]
b = []
for item in a:
for i in range(0, len(a)):
if item[0] == a[i][0]:
break
else:
if item in b:
continue
else:
b.append(item)
产量
(xenial)vash@localhost:~/pcc/10$ python3 remove_help.py [[1, 2], [1, 0], [2, 4], [3, 5]] [[2, 4], [3, 5]]
实现了你的目标没有复杂的方法! 请享用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.