我有一堆巨大的整数列表。这些列表可能以几个零开头或结尾。
有没有一种简单的方法可以去除列表左侧或右侧的零?
类似于字符串的 lstrip()
或 rstrip()
的东西?
数据看起来像
[0,0,0,1,2,3,4]
或
[1,2,3,4,0,0,0]
我必须能够单独lstrip()
或rstrip()
。我不需要列表两边的 strip 。
最佳答案
你可以使用 itertools.dropwhile()
:
>>> L = [0, 0, 1, 1, 2, 2, 0]
>>> list(itertools.dropwhile(lambda x: x == 0, L))
[1, 1, 2, 2, 0]
关于python - lstrip(), rstrip() 用于列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8784813/