use*_*312 2 python list unique
我知道这个问题已被问过很多次,但我不是问如何从列表中删除重复的元素,我也想删除重复的元素.
例如,如果我有一个列表:
x = [1, 2, 5, 3, 4, 1, 5]
Run Code Online (Sandbox Code Playgroud)
我希望列表是:
x = [2, 3, 4] # removed 1 and 5 since they were repeated
Run Code Online (Sandbox Code Playgroud)
我不能使用set,因为那将包括1和5.
我应该用Counter吗?有没有更好的办法?
Mah*_*der 10
这应该使用Counter对象完成.这是微不足道的.
from collections import Counter
x = [k for k, v in Counter([1, 2, 5, 3, 4, 1, 5]).iteritems() if v == 1]
print x
Run Code Online (Sandbox Code Playgroud)
输出:
[2, 3, 4]
Run Code Online (Sandbox Code Playgroud)