过滤列表以只留下出现一次的对象 [英] Filter a list to only leave objects that occur once

查看:11
本文介绍了过滤列表以只留下出现一次的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤这个列表,

[0, 1, 1, 2, 2]

只能离开

[0]

我正在努力以pythonic"的方式做到这一点.没有嵌套循环可以吗?

I'm struggling to do it in a 'pythonic' way. Is it possible without nested loops?

推荐答案

这是另一种面向字典的方式:

Here's another dictionary oriented way:

l = [0, 1, 1, 2, 2]
d = {}
for i in l: d[i] = i in d

[k for k in d if not d[k]]  # unordered, loop over the dictionary
[k for k in l if not d[k]]  # ordered, loop over the original list

这篇关于过滤列表以只留下出现一次的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆