我正在尝试删除我从列表中创建的位数组的前导0。我想做的是:
while binPayload[0] == 0:
del binPayload[0]
然而,断路器正在抛出:
IndexError: list assignment index out of range.
答案 0 :(得分:2)
每次索引之前,都应该检查列表是否为空。自an empty list is considered false起,您就可以执行以下操作:
while binPayload and binPayload[0] == 0:
del binPayload[0]
答案 1 :(得分:1)
试试这个:
import itertools as it
a = [0, 0, 0, 0, 1, 1, 1, 1]
list(it.dropwhile(lambda x: x == 0, a))
=> [1, 1, 1, 1]