【问题标题】:Is there a way to use strip() on a list object? - python有没有办法在列表对象上使用 strip() ? - Python
【发布时间】:2017-03-23 07:28:06
【问题描述】:

现在我有一个这样的列表对象:

lst = [None, None, 'one', None, 'two', None]

我正在尝试对其执行 strip() 并得到如下结果:

strip(lst)

>> ['one', None, 'two']

left_strip(lst)

>> ['one', None, 'two', None]

有没有优雅的方式来做到这一点?

ps:感谢 4 @woockashek 的建议,我已经更改了 lst
[None, None, 'one','two', None][None, None, 'one', None, 'two', None]

【问题讨论】:

  • 不是一个单一的解决尝试,只是一个问题转储,这个有一个赞成票?很酷的投票。
  • 现在还不清楚您想要实现什么。 [None, 'one', None, 'two', None] 呢。你期待['one', None, 'two']['one', 'two']

标签: python


【解决方案1】:

要获得类似left_strip 的行为,您需要从itertools 导入dropwhile

>>> lst=[None, None, 'one', None, 'two', None, None]
>>> from itertools import dropwhile
>>> def left_strip(lst):
        return list(dropwhile(lambda x : x is None, lst))
>>> left_strip(lst)
['one',None, 'two', None, None]

要获得类似right_strip 的行为:

>>> from itertools import dropwhile
>>> def right_strip(lst):
        return list(reversed(left_strip(reversed(lst))))
>>> right_strip(lst)
[None, None, 'one', None, 'two']

要获得strip,请依次运行:

>>> left_strip(right_strip(lst))
['one', None, 'two']

【讨论】:

  • if item is not None,以防 OP 想要保留潜在的零值。
  • @SebastianWozny 这不是“Pythonic”的问题,它只是做了不同的事情。
  • 您对strip 的回答不正确。如果您在12 之间有None 怎么办?
  • 现在应该解决所有问题。
【解决方案2】:

您可以使用 itertools.dropwhile 来模拟 lstrip:

def lstrip(list):
    return list(itertools.dropwhile(lambda x : x is None, list))

lst = [None, None, 'one', 'two', None]
lstrip(lst)
>> ['one', 'two', None]

rstrip 可以以相同的方式实现,但将之前的列表颠倒 在使用 dropwhile 之后

def rstrip(list):
    return list(reversed(lstrip(reversed(list))))

【讨论】:

    【解决方案3】:

    有一种过滤方法:

    lst = filter(None, lst)
    

    但如果您的列表中有 0 个值,它也会删除它们

    要解决此问题,您必须编写自己的过滤器方法

    def FilterNone(arg):
        return arg is not None
    
    filtered_list = filter(FilterNone, lst)
    

    【讨论】:

    • 这不是正确的答案。如果你通过lst = [None, 1, None, 2, None]怎么办?
    • 示例不清楚,所以我认为这种情况永远不会发生。我还在 OP 的帖子中询问了这样的例子,但没有得到答案。
    • 如果你知道string.strip() 做了什么(从两端删除空格),你可以假设这是期望的行为(或者当然,你需要在这里去掉None)。
    • 我尝试在字里行间阅读。提及strip 并不能保证用户需要它的确切行为,这就是我等待他更新的原因。
    【解决方案4】:

    这里是自然left_strip()

    def left_strip(items):
        for i,x in enumerate(items):
            if x is not None: return items[i:]
        return [] #in case all are None
    

    例如,

    >>> left_strip(lst)
    ['one', 'two', None]
    

    【讨论】:

    • 如果你传递空列表或者列表只包含None怎么办?
    • @Fejs 不错。固定。
    【解决方案5】:

    你可以像这样使用过滤器:

    lst = [None, None, 'one', 'two', None]
    list(filter(None.__ne__, lst))
    

    【讨论】:

    • 这个在python2.7上给AttributeError: 'NoneType' object has no attribute '__ne__',所以很可能是python3的解决方案
    • 这不是正确的答案。如果你通过lst = [None, 1, None, 2, None] 怎么办?另外,您没有left_strip 的解决方案。
    【解决方案6】:

    一种方法是:

    def right_strip(lst):
        for i in range(len(lst) - 1, -1, -1):
            if lst[i] != None:
                break
        else:
            return []
        return lst[:i + 1]
    
    def left_strip(lst):
        for i in range(len(lst)):
            if lst[i] != None:
                break
        else:
            return []
        return lst[i:]
    
    def strip(lst):
        return left_strip(right_strip(lst))
    

    【讨论】:

      【解决方案7】:

      没有得到你真正想做的事?您可以使用 lst[2:4] 和 lst[2:]。

      更新:使用集合

      如果您只想拥有不是“无”的值,那么您可以使用如下所示的设置:

      lst = [None, None, 'one', 'two', None]
      lst1=[None]
      print list(set(lst)-set(lst1))
      

      【讨论】:

      • 这个答案是不正确的,除非你有这个输入。这是一个可怕的假设。
      • @Fejs 我已经表示不清楚要问什么。答案是基于左条纹的假设。
      【解决方案8】:

      你可以试试:

      def left_strip(lst):
          for i, e in enumerate(lst):
              if e is not None:
                  return lst[i:]
          return []
      
      
      def right_strip(lst):
          for i, e in reversed(list(enumerate(lst))):
              if e is not None:
                  return lst[:i+1]
          return []
      
      print left_strip(right_strip(lst))
      

      您可以在remove None value from a list without removing the 0 value阅读更多内容

      【讨论】:

      • 首先,您没有第二个功能的答案 (left_strip)。其次,这个答案是不正确的。如果您在12 之间有None 怎么办?
      猜你喜欢
      • 2016-08-06
      • 2020-11-01
      • 2013-07-19
      • 2013-07-18
      • 2018-07-11
      • 2021-11-12
      • 2021-11-22
      • 2020-03-25
      • 1970-01-01
      相关资源
      最近更新 更多