让我们假设我有一个整数列表:
mylist = [101, 102, 103, 104, 105, 106]
现在我需要创建每个可能的子列表分区(保留顺序):
sublists = [([101], [102, 103, 104, 105, 106]),
([101, 102], [103, 104, 105, 106]),
([101, 102, 103], [104, 105, 106]),
...
([101, 102], [103, 104], [105, 106]),
...
([101], [102, 103, 104], [105], [106]),
...
([101], [102], [103], [104], [105], [106])]
有什么想法吗? itertools
会有帮助吗?
答案 0 :(得分:2)
您正在创建切片点;你是否在当前元素之后进行切片。您可以使用布尔值生成这些:
from itertools import product
def sublists(lst):
for doslice in product([True, False], repeat=len(lst) - 1):
slices = []
start = 0
for i, slicehere in enumerate(doslice, 1):
if slicehere:
slices.append(lst[start:i])
start = i
slices.append(lst[start:])
yield slices
演示:
>>> from pprint import pprint
>>> mylist = [101, 102, 103, 104, 105, 106]
>>> pprint(list(sublists(mylist)))
[[[101], [102], [103], [104], [105], [106]],
[[101], [102], [103], [104], [105, 106]],
[[101], [102], [103], [104, 105], [106]],
[[101], [102], [103], [104, 105, 106]],
[[101], [102], [103, 104], [105], [106]],
[[101], [102], [103, 104], [105, 106]],
[[101], [102], [103, 104, 105], [106]],
[[101], [102], [103, 104, 105, 106]],
[[101], [102, 103], [104], [105], [106]],
[[101], [102, 103], [104], [105, 106]],
[[101], [102, 103], [104, 105], [106]],
[[101], [102, 103], [104, 105, 106]],
[[101], [102, 103, 104], [105], [106]],
[[101], [102, 103, 104], [105, 106]],
[[101], [102, 103, 104, 105], [106]],
[[101], [102, 103, 104, 105, 106]],
[[101, 102], [103], [104], [105], [106]],
[[101, 102], [103], [104], [105, 106]],
[[101, 102], [103], [104, 105], [106]],
[[101, 102], [103], [104, 105, 106]],
[[101, 102], [103, 104], [105], [106]],
[[101, 102], [103, 104], [105, 106]],
[[101, 102], [103, 104, 105], [106]],
[[101, 102], [103, 104, 105, 106]],
[[101, 102, 103], [104], [105], [106]],
[[101, 102, 103], [104], [105, 106]],
[[101, 102, 103], [104, 105], [106]],
[[101, 102, 103], [104, 105, 106]],
[[101, 102, 103, 104], [105], [106]],
[[101, 102, 103, 104], [105, 106]],
[[101, 102, 103, 104, 105], [106]],
[[101, 102, 103, 104, 105, 106]]]
如果要删除最后一个条目(包含其中只有一个列表的列表,依次包含所有元素),请将最后两行替换为:
if start:
slices.append(lst[start:])
yield slices
答案 1 :(得分:0)
这是一个有趣的问题!虽然Martijn给了an elegant answer,但我仍然想发布我的另一个角度解决这个问题 - Divide&征服:
def sublist_gen(ls):
n = len(ls)
if n == 1:
yield [ls]
else:
for left in sublist_gen(ls[0:n/2]):
for right in sublist_gen(ls[n/2:n]):
yield left + right
yield left[0:-1] + [left[-1] + right[0]] + right[1:]
mylist = [101, 102, 103, 104, 105, 106]
for sublist in sublist_gen(mylist):
print(sublist)
结果:
[[101], [102], [103], [104], [105], [106]]
[[101], [102], [103, 104], [105], [106]]
[[101], [102], [103], [104, 105], [106]]
[[101], [102], [103, 104, 105], [106]]
[[101], [102], [103], [104], [105, 106]]
[[101], [102], [103, 104], [105, 106]]
[[101], [102], [103], [104, 105, 106]]
[[101], [102], [103, 104, 105, 106]]
[[101, 102], [103], [104], [105], [106]]
[[101, 102], [103, 104], [105], [106]]
[[101, 102], [103], [104, 105], [106]]
[[101, 102], [103, 104, 105], [106]]
[[101, 102], [103], [104], [105, 106]]
[[101, 102], [103, 104], [105, 106]]
[[101, 102], [103], [104, 105, 106]]
[[101, 102], [103, 104, 105, 106]]
[[101], [102, 103], [104], [105], [106]]
[[101], [102, 103, 104], [105], [106]]
[[101], [102, 103], [104, 105], [106]]
[[101], [102, 103, 104, 105], [106]]
[[101], [102, 103], [104], [105, 106]]
[[101], [102, 103, 104], [105, 106]]
[[101], [102, 103], [104, 105, 106]]
[[101], [102, 103, 104, 105, 106]]
[[101, 102, 103], [104], [105], [106]]
[[101, 102, 103, 104], [105], [106]]
[[101, 102, 103], [104, 105], [106]]
[[101, 102, 103, 104, 105], [106]]
[[101, 102, 103], [104], [105, 106]]
[[101, 102, 103, 104], [105, 106]]
[[101, 102, 103], [104, 105, 106]]
[[101, 102, 103, 104, 105, 106]]