我有一个 list ,比如说
listx = [1, 2, 0, 5, 2, 3, 0, 3, 4, 2, 3, 5, 6, 0, 4]
如何将列表拆分为不包含 0 的连续部分?例如,此列表将分为
separated = [[1, 2], [5, 2, 3], [3, 4, 2, 3, 5, 6], [4]]
最佳答案
import itertools
In [69]: L = [1, 2, 0, 5, 2, 3, 0, 3, 4, 2, 3, 5, 6, 0, 4]
In [70]: [list(i[1]) for i in itertools.groupby(L, bool) if i[0]]
Out[70]: [[1, 2], [5, 2, 3], [3, 4, 2, 3, 5, 6], [4]]
关于python - 将列表拆分为单独的列表 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822726/