【问题标题】:Python remove null items from listPython从列表中删除空项目
【发布时间】:2015-04-21 04:36:26
【问题描述】:

我在 python 中为DynamoBIM 创建了以下列表:

a = [["f", "o", "c"], [null, "o", null], [null, "o", null]]

我想从这个列表中删除空项目来创建这个列表:

a = [["f", "o", "c"], ["o"], ["o"]]

我尝试了list.remove(x)、filters、for-loops 和许多其他方法,但似乎无法摆脱这些虫子。

我该怎么做?

【问题讨论】:

  • 您的列表的嵌套程度如何?
  • 什么是null?你是说None?
  • 谢谢大家!这些都是很好的答案;但这似乎是 Dynamo 的问题。正如@Arun 指出的那样,'null' 不在 Python 中。 Dynamo 创建这些 'null' 项目来代替 'None' 并且它们在某种程度上无法识别为字符串。如果有人在 Dynamo 中使用 Python 时遇到此问题,我使用了“Object.IsNull”和“List.FilterByBoolMask”组件来删除“null”项。 !Valid XHTML.

标签: python list filter null


【解决方案1】:

假设您的意思是 None 为 null,您可以使用列表推导:

>>> null = None
>>> nested_list = [["f", "o", "c"], [null, "o", null], [null, "o", null]]
>>> [[x for x in y if x] for y in nested_list]
[['f', 'o', 'c'], ['o'], ['o']]

如果 null 是其他值,您可以更改上述内容以将 null 的值设置为其他值,并将理​​解更改为:

>>> null = None # Replace with your other value
>>> [[x for x in y if x != null] for y in nested_list]
[['f', 'o', 'c'], ['o'], ['o']]

【讨论】:

    【解决方案2】:
    a = [["f", "o", "c"], [None, "o", None], [None, "o", None]]
    l = []
    for i in a:
        l.append(filter(lambda x: x is not None, i))
    
    print (l)
    
    [['f', 'o', 'c'], ['o'], ['o']]
    

    【讨论】:

      【解决方案3】:

      实际上Null不在python中,它们应该是字符串

      list_value = [["f", "o", "c"], ['null', "o", 'null'], ['null', "o", 'null']]
      
      [filter(lambda x: x!='null' and x!=None, inner_list) for inner_list in list_value]
      
      [['f', 'o', 'c'], ['o'], ['o']]
      

      你也可以通过嵌套列表理解来解决:

      [[for i in inner_list if i!='null' and not i] for inner_list in list_value]
      

      【讨论】:

        【解决方案4】:

        假设您的意思是None,您可以尝试使用filter() 函数:

        a = [["f", "o", "c"], [None, "o", None], [None, "o", None]]
        print [filter(None,x) for x in a] 
        

        >>> 
        [['f', 'o', 'c'], ['o'], ['o']]
        

        【讨论】:

          【解决方案5】:

          您可以使用简单的列表推导:

          >>> a = [["f", "o", "c"], [None, "o", null], [null, "o", None]]
          >>> a = [[sub for sub in item if sub] for item in a]
          >>> a
          [['f', 'o', 'c'], ['o'], ['o']]
          >>> 
          

          这相当于:

          a = [["f", "o", "c"], [None, "o", null], [null, "o", None]]
          new = []
          for item in a:
              _temp = []
              for sub in item:
                  if sub:
                      _temp.append(sub)
              new.append(_temp)
          
          a = new
          

          >>> a = [["f", "o", "c"], [None, "o", null], [null, "o", None]]
          >>> new = []
          >>> for item in a:
          ...     _temp = []
          ...     for sub in item:
          ...         if sub:
          ...             _temp.append(sub)
          ...     new.append(_temp)
          ... 
          >>> a = new
          >>> a
          [['f', 'o', 'c'], ['o'], ['o']]
          >>> 
          

          【讨论】:

            【解决方案6】:

            Python 中的“None”和 NaN(“非数字”)之间存在区别,有时通常称为“null”。 NaN 的数据类型一般是浮点数。因此,如果您正在使用仅包含字符串的列表,就像这里的情况一样,一个想法是使用列表推导过滤字符串。

            a = ["f", "o", "c", np.nan]
            b = [c for c in a if type(c)==str] 
            

            【讨论】:

              猜你喜欢
              • 2011-01-17
              • 2022-12-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-09-29
              • 2013-08-03
              相关资源
              最近更新 更多