x = ['Gaga', 'Gaga', 'Lam', 'Reem', 'Pal', 'Gaga','Lam']
from collections import Counter
singles = {k for k, v in Counter(x).items() if v == 1}
y = [i for i in x if i not in singles]
loc = [int(i in singles) for i in x]
print(y, loc, sep='\n')
['Gaga', 'Gaga', 'Lam', 'Gaga', 'Lam']
[0, 0, 0, 1, 1, 0, 0]
from collections import Counter
x = ['Gaga', 'Gaga', 'Lam', 'Reem', 'Pal', 'Gaga','Lam']
c = Counter(x)
new_values = [item for item in x if c[item] > 1]
indexes = [1 if c[item] == 1 else 0 for item in x]
print(new_values)
print(indexes)
具有} 函数的可选的一个线性:
pandas
模块及其^{要删除所有非重复项:
您可以使用^{} 并隔离只出现一次的项。然后用列表理解你想要的结果。解决方案总体上是O(n),尽管它涉及3个过程。你知道吗
您可以使用
Counter
类来实现以下目的:输出为:
相关问题 更多 >
编程相关推荐