有没有办法在列表对象上使用strip()? - 蟒蛇

时间:2016-11-09 13:07:59

标签: python

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

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]

8 个答案:

答案 0 :(得分:6)

要获得<?php if(isset($_POST['email'])) { $email_to = "info@xxx.co.uk"; $email_subject = 'Thank you for filling in the form!'; $email_from = "info@xxx.co.uk"; function died($error) { include("sorry.htm"); ("Location:sorry.htm"); die(); } if(!isset($_POST['formtext']) } $form = $_POST['formtext']; $error_message = ""; $email_message .= " ".clean_string($formtext)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); @mail($email_customer, $email_subject, $email_message, $headers); ?> <?php include("thanks.htm"); ("Location:thanks.htm"); ?> <?php } ?> 之类的行为,您需要从itertools导入left_strip

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

答案 1 :(得分:4)

您可以使用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))))

答案 2 :(得分:1)

有一个过滤方法:

lst = filter(None, lst)

但是如果列表中有值,它也会删除0值

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

def FilterNone(arg):
    return arg is not None

filtered_list = filter(FilterNone, lst)

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

这是一个自然的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]

答案 5 :(得分:0)

您可以尝试:

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

中阅读更多内容

答案 6 :(得分:0)

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

更新:使用设置

如果您只想使用非“无”的值,则使用如下所示的设置:

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

答案 7 :(得分:0)

这样做的一种方法是:

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))
相关问题