Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除仅出现在列表开头的0_Python_List - Fatal编程技术网

Python 删除仅出现在列表开头的0

Python 删除仅出现在列表开头的0,python,list,Python,List,我有一个列表,它的开头有0,我想删除。0确实出现在数据的其他地方,但我想保留这些 l=[0, 0, 0, 0, 151, 199, 149, 147, 281, 133, 166, 162, 0, 353, 867, 1060, 525, 1031, 420, 0, 832, 1114, 869, 531, 546, 555, 520, 679, 715, 669, 555, 888, 605, 809, 0, 514] 需要成为: l=[151, 199, 149, 147, 281, 13

我有一个列表,它的开头有0,我想删除。0确实出现在数据的其他地方,但我想保留这些

l=[0, 0, 0, 0, 151, 199, 149, 147, 281, 133, 166, 162, 0, 353, 867, 1060, 525, 1031, 420, 0, 832, 1114, 869, 531, 546, 555, 520, 679, 715, 669, 555, 888, 605, 809, 0, 514]
需要成为:

l=[151, 199, 149, 147, 281, 133, 166, 162, 0, 353, 867, 1060, 525, 1031, 420, 0, 832, 1114, 869, 531, 546, 555, 520, 679, 715, 669, 555, 888, 605, 809, 0, 514]
用于删除这些零:

from itertools import dropwhile
import operator

l = list(dropwhile(operator.not_, l))
这将仅删除初始的0值;或者更确切地说,所有假y值都使用

演示:


可以使用list.index()和next()方法:

>>> from itertools import dropwhile
>>> import operator
>>> l=[0, 0, 0, 0, 151, 199, 149, 147, 281, 133, 166, 162, 0, 353, 867, 1060, 525, 1031, 420, 0, 832, 1114, 869, 531, 546, 555, 520, 679, 715, 669, 555, 888, 605, 809, 0, 514]
>>> list(dropwhile(operator.not_, l))
[151, 199, 149, 147, 281, 133, 166, 162, 0, 353, 867, 1060, 525, 1031, 420, 0, 832, 1114, 869, 531, 546, 555, 520, 679, 715, 669, 555, 888, 605, 809, 0, 514]
L=[0, 0, 1, 2, 'a', 3, 0, 0, 9]

noLeadingZeros = L[L.index(next(i for i in L if i!=0)):]