首页 文章

如果不删除0值,则从列表中删除无值

提问于
浏览
172

This was my source I started with.

我的列表

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值?

10 回答

  • 96
    >>> 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]
    

    只是为了好玩,这里是你如何能够在不使用 lambda 的情况下调整 filter ,(我不会仅仅出于科学目的而使用't recommend this code - it')

    >>> 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]
    
  • 257

    FWIW,Python 3使这个问题变得容易:

    >>> L = [0, 23, 234, 89, None, 0, 35, 9]
    >>> list(filter(None.__ne__, L))
    [0, 23, 234, 89, 0, 35, 9]
    

    在Python 2中,您将使用列表推导:

    >>> [x for x in L if x is not None]
    [0, 23, 234, 89, 0, 35, 9]
    
  • 11

    对于Python 2.7(参见Raymond的回答,相当于Python 3):

    想要知道“不是非”的东西在python(以及其他OO语言)中是如此常见,在我的Common.py中(我使用“来自Common import *”导入每个模块),我包括以下行:

    def exists(it):
        return (it is not None)
    

    然后从列表中删除无元素,只需执行以下操作:

    filter(exists, L)
    

    我觉得这比相应的列表理解更容易阅读(Raymond表示,作为他的Python 2版本) .

  • 15

    使用列表理解可以按如下方式完成:

    l = [i for i in my_list if i is not None]
    

    l的值是:

    [0, 23, 234, 89, 0, 35, 9]
    
  • 1

    @jamylak的回答非常好,但如果您不想导入几个模块来执行这个简单的任务,那么就地编写自己的 lambda

    >>> L = [0, 23, 234, 89, None, 0, 35, 9]
    >>> filter(lambda v: v is not None, L)
    [0, 23, 234, 89, 0, 35, 9]
    
  • 5

    Iteration vs Space ,使用可能是一个问题 . 在不同的情况下,分析可能显示为"faster"和/或"less memory"密集 .

    # first
    >>> 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, ...]
    
    # second
    >>> L = [0, 23, 234, 89, None, 0, 35, 9]
    >>> for i in range(L.count(None)): L.remove(None)
    [0, 23, 234, 89, 0, 35, 9, ...]
    

    first 方法(也由@jamylak@Raymond Hettinger@Dipto建议)在内存中创建一个重复列表,对于包含少量 None 条目的大型列表而言,这可能代价很高 .

    second 方法一次通过列表,然后每次再次到达 None . 这可能会减少内存密集,并且随着时间的推移,列表会变小 . 列表大小的减少可能会加速前面的大量 None 条目,但最糟糕的情况是如果后面有很多 None 条目 .

    并行化和就地技术是其他方法,但每种方法都有自己的Python复杂性 . 了解数据和运行时用例以及对程序进行概要分析是从密集型操作或大型数据开始的地方 .

    在常见情况下,选择任何一种方法都可能无关紧要 . 它更像是符号的偏好 . 事实上,在那些不常见的情况下, numpycython 可能是值得的替代品,而不是试图微观管理Python优化 .

  • 2
    from operator import is_not
    from functools import partial   
    
    filter_null = partial(filter, partial(is_not, None))
    
    # A test case
    L = [1, None, 2, None, 3]
    L = list(filter_null(L))
    
  • 8

    如果它都是列表列表,你可以修改先生@Raymond的回答

    但是,对于python 2, L = [ [None], [123], [None], [151] ] no_none_val = list(filter(None.__ne__, [x[0] for x in L] ) )

    no_none_val = [x[0] for x in L if x[0] is not None] """ Both returns [123, 151]"""

    如果变量不是None,则列表中的变量<< list_indice [0] >>

  • 2

    说列表如下

    iterator = [None, 1, 2, 0, '', None, False, {}, (), []]
    

    这将只返回那些 bool(item) is True 的项目

    print filter(lambda item: item, iterator)
    # [1, 2]
    

    这相当于

    print [item for item in iterator if item]
    

    要过滤无:

    print filter(lambda item: item is not None, iterator)
    # [1, 2, 0, '', False, {}, (), []]
    

    相当于:

    print [item for item in iterator if item is not None]
    

    获取评估为False的所有项目

    print filter(lambda item: not item, iterator)
    # Will print [None, '', 0, None, False, {}, (), []]
    
  • -2

    此解决方案使用 while 而不是 for

    value = None
    while value in yourlist:
        yourlist.remove(value)
    

相关问题