gpt4 book ai didi

python - lstrip(), rstrip() 用于列表

转载 作者:太空狗 更新时间:2023-10-29 22:23:33 25 4
gpt4 key购买 nike

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

有没有一种简单的方法可以去除列表左侧或右侧的零?类似于字符串的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com