如果有一个列表,其中的元素是[2,2,3,2,2].我想找到唯一的元素,这次是3.
我想我可以用count()方法和一些循环来做到这一点,但我想知道是否有更简单有效的方法来做到这一点.
你可以使用collections.Counter:
>>> import collections
>>> l = [2,2,3,2,2]
>>> next(k for k, v in collections.Counter(l).items() if v == 1)
3
Run Code Online (Sandbox Code Playgroud)