从列表中删除 None 值而不删除 0 值 [英] remove None value from a list without removing the 0 value
本文介绍了从列表中删除 None 值而不删除 0 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的列表
L = [0, 23, 234, 89, None, 0, 35, 9]
当我运行这个时:
L = filter(None, L)
我得到这个结果
[23, 234, 89, 35, 9]
但这不是我需要的,我真正需要的是:
[0, 23, 234, 89, 0, 35, 9]
因为我正在计算数据的百分位数,而 0 有很大的不同.
如何在不删除 0 值的情况下从列表中删除 None 值?
解决方案
>>>L = [0, 23, 234, 89, 无, 0, 35, 9]>>>[x 代表 L 中的 x,如果 x 不是 None][0, 23, 234, 89, 0, 35, 9]
只是为了好玩,这里介绍了如何在不使用 lambda
的情况下调整 filter
来执行此操作,(我不推荐此代码 - 它仅用于科学目的)
This was my source I started with.
My List
L = [0, 23, 234, 89, None, 0, 35, 9]
When I run this :
L = filter(None, L)
I get this results
[23, 234, 89, 35, 9]
But this is not what I need, what I really need is :
[0, 23, 234, 89, 0, 35, 9]
Because I'm calculating percentile of the data and the 0 make a lot of difference.
How to remove the None value from a list without removing 0 value?
解决方案
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]
Just for fun, here's how you can adapt filter
to do this without using a lambda
, (I wouldn't recommend this code - it's just for scientific purposes)
>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(partial(is_not, None), L)
[0, 23, 234, 89, 0, 35, 9]
这篇关于从列表中删除 None 值而不删除 0 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文