python - 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?
解决方案
您可以计算元素的频率并选择频率为 1 的元素,如下所示。
from collections import Counter
arr = ...
unique = [k for k,v in Counter(arr).items() if v == 1]
推荐阅读
- html - 网格内的图像对齐
- javascript - Javascript 不等待“then”表单提交
- javascript - Vue 3:为什么在 Vue 3 模板中引用时 dom 引用值未定义
- cron - cronjob 在完成之前被杀死
- authentication - Kubernetes:如何让另一个人使用编辑 clusterRole 权限访问 Kubernetes 集群?
- javascript - 通过数组访问 javascript 对象值
- spring-batch - SCDF服务器双数据库连接错误
- javascript - firebase.functions() 在 React 中未定义,而 firebase.firestore() 可以工作
- notepad++ - 在记事本++中为特定格式添加引号
- css - 自定义轮廓和输入文本颜色问题