How to slice list into contiguous groups of non-zero integers in Python

后端 未结 5 564
旧巷少年郎
旧巷少年郎 2020-12-31 20:50

Can\'t seem to find a clue to this online and can\'t figure it out myself so:

How would I go about slicing a list so that I return a list of slices of contiguous non

5条回答
  •  旧时难觅i
    2020-12-31 21:27

    import itertools
    [ list(x[1]) for x in itertools.groupby(data, lambda x: x == 0) if not x[0] ]
    

提交回复
热议问题