# 根据Python中的一组索引将列表拆分成不同的部分

``indexes = [5, 12, 17] list = range(20)` `

` `part1 = list[:5] part2 = list[5:12] part3 = list[12:17] part4 = list[17:]` `

` `def partition(alist, indices): return [alist[i:j] for i, j in zip([0]+indices, indices+[None])]` `

` `from itertools import izip, chain def partition(alist, indices): pairs = izip(chain([0], indices), chain(indices, [None])) return (alist[i:j] for i, j in pairs)` `

` `import numpy partition = numpy.split` `

` `indexes = [5, 12, 17] list = range(20) output = [] prev = 0 for index in indexes: output.append(list[prev:index]) prev = index output.append(list[indexes[-1]:]) print output` `

` `[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16], [17, 18, 19]]` `

` `>>> def parts(list_, indices): ... indices = [0]+indices+[len(list_)] ... return [list_[v:indices[k+1]] for k, v in enumerate(indices[:-1])]` `

## 替代方法

` `>>> from itertools import islice >>> def parts(list_, indices): ... i = iter(list_) ... return [list(islice(i, n)) for n in chain(indices, [None])]` `
` `>>> def burst_seq(seq, indices): ... startpos = 0 ... for index in indices: ... yield seq[startpos:index] ... startpos = index ... yield seq[startpos:] ... >>> list(burst_seq(range(20), [5, 12, 17])) [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16], [17, 18, 19]] >>> list(burst_seq(range(20), [])) [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]] >>> list(burst_seq(range(0), [5, 12, 17])) [[], [], [], []] >>>` `

Maxima mea culpa：它使用`for`语句，而不是像itertools，zip（），None作为一个标记，列表parsing，whizzbang的东西…

😉

` `indices = [5, 12, 17] input = range(20) output = [] reduce(lambda x, y: output.append(input[x:y]) or y, indices + [len(input)], 0) print output` `

` `def partition(list_, indexes): if indexes[0] != 0: indexes = [0] + indexes if indexes[-1] != len(list_): indexes = indexes + [len(list_)] return [ list_[a:b] for (a,b) in zip(indexes[:-1], indexes[1:])]` `

Cide's使数组的三个副本：[0] +索引副本，（[0] +索引）+ []再次复制，并且索引[： – 1]将第三次复制。 Il-Bhima制作五份。 （当然，我不计算回报价值。）

` `def iterate_pairs(lst, indexes): prev = 0 for i in indexes: yield prev, i prev = i yield prev, len(lst) def partition(lst, indexes): for first, last in iterate_pairs(lst, indexes): yield lst[first:last] indexes = [5, 12, 17] lst = range(20) print [l for l in partition(lst, indexes)]` `

` `for first, last in iterate_pairs(lst, indexes): for i in range(first, last): lst[i] = first print lst # [0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 12, 12, 12, 12, 12, 17, 17, 17]` `

（这就是为什么我将索引传递给iterate_pairs的原因。如果你不关心这个，你可以删除这个参数，最后一行是“yield prev，None”，这是所有partition（）的需要。

` `def partition(l, indexes): result, indexes = [], indexes+[len(l)] reduce(lambda x, y: result.append(l[x:y]) or y, indexes, 0) return result` `

` `>>> partition([1,2,3,4,5], [1, -1]) [[1], [2, 3, 4], [5]] >>>` `

` `indices = [5, 12, 17] input = range(20) output = [] for i in reversed(indices): output.append(input[i:]) input[i:] = [] output.append(input) while len(output): print output.pop()` `