Contiguous subsequences? Paging Dr. Groupby, Dr. itertools.groupby:>>> from itertools import groupby>>> l = [240,200,160,4,0,0,0,0,4,4,4,0,0,0,1,1,1,1]>>> [list(g) for k,g in groupby(l, lambda x: x != 0) if k][[240, 200, 160, 4], [4, 4, 4], [1, 1, 1, 1]],或者即使我们利用bool(0)是False而bool(any other integer)是True的事实:or even if we take advantage of the fact bool(0) is False and bool(any other integer) is True:>>> [list(g) for k,g in groupby(l, bool) if k][[240, 200, 160, 4], [4, 4, 4], [1, 1, 1, 1]] 这篇关于数组的子序列不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 03:39