首页 > 解决方案 > How to get only unique values from a list in Python

问题描述

So i want to do choose only the unique character of an array , the desired output is suppose to be :

Input : 4,5,6,4,2,2,9

Output : 5,6,9

I tried these code :

arr = [4,5,6,4,2,2,9]
unique = list(set(arr))

But the output is : 4,5,6,2,9

Is it possible to do it without numpy?

标签: python

解决方案


您可以计算元素的频率并选择频率为 1 的元素,如下所示。

from collections import Counter
arr = ...
unique = [k for k,v in Counter(arr).items() if v == 1]

推荐阅读