本文介绍了从列表中删除无值,而不删除0值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
。
我的清单
L = [0,23,234,89,无, 0,35,9]
当我运行这个:
L =过滤器(无,L)
我得到这个结果
[23,234,89,35,9]
但是这不是我所需要的,我真正需要的是:
$ b $
[0,23,234,89,0,35,9]
因为我正在计算数据的百分位数,0会产生很大的差别。
如何从列表中删除None值而不删除0值? p>
解决方案
>>> L = [0,23,234,89,无,0,35,9]
>>> [x for x in L如果x不是None]
[0,23,234,89,0,35,9]
只是为了好玩,下面是如何使用 filter
来做到这一点,而不使用 lambda $ c
$ b
>>> $ c>,(我不会推荐这个代码 - 它只是为了科学目的) from operator import is_not
>>> from functools import partial
>>> L = [0,23,234,89,无,0,35,9]
>>>过滤器(部分(is_not,无),L)
[0,23,234,89,0,35,9]
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]
这篇关于从列表中删除无值,而不删除0值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!