lstrip(),rstrip()用于列表 [英] lstrip(), rstrip() for lists

查看:74
本文介绍了lstrip(),rstrip()用于列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆巨大的整数列表.这些列表可能以两个零开头或结尾.

I have a bunch of huge lists with integers. These lists may start or end with a couple of zeroes.

是否有一种简单的方法可以删除列表左侧或右侧的零? 类似于lstrip()rstrip()的字符串吗?

Is there an easy way for strip either the zeroes on the left or right side from the list? Something analogous to lstrip() or rstrip() for strings?

数据看起来像

[0,0,0,1,2,3,4]

[1,2,3,4,0,0,0]

我必须能够分别lstrip()rstrip().我不需要列表两边的小条.

I must be able to individually lstrip() or rstrip(). I do not need a strip from both sides of the list.

推荐答案

您可以使用 itertools.dropwhile() :

You could use itertools.dropwhile():

>>> L = [0, 0, 1, 1, 2, 2, 0]
>>> list(itertools.dropwhile(lambda x: x == 0, L))
[1, 1, 2, 2, 0]

这篇关于lstrip(),rstrip()用于列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆