给定python中的列表(或者java,我试图用两种语言来做),我怎样才能获得将列表拆分成不同分组的所有不同方法,因为每个分组必须至少具有一定的大小?我认为获得分割位置的组合是最好的方法。
列表和最小尺寸的示例输入是
[1,2,3], 2
,相应的输出应为
[[1,2], [1,3], [2,3], [1,2,3]]
答案 0 :(得分:2)
在Python中,你可以递归地执行此操作:
def partition(lst, minsize=1):
yield [lst]
for n in range(minsize, len(lst)-minsize+1):
for p in partition(lst[n:], minsize):
yield [lst[:n]] + [l for l in p]
例如:
>>> lst = [1, 2, 3, 4, 5, 6, 7]
>>> partition(lst, 3)
[[[1, 2, 3, 4, 5, 6, 7]],
[[1, 2, 3], [4, 5, 6, 7]],
[[1, 2, 3, 4], [5, 6, 7]]]
>>> list(partition(lst, 2))
[[[1, 2, 3, 4, 5, 6, 7]], [[1, 2], [3, 4, 5, 6, 7]],
[[1, 2], [3, 4], [5, 6, 7]], [[1, 2], [3, 4, 5], [6, 7]],
[[1, 2, 3], [4, 5, 6, 7]], [[1, 2, 3], [4, 5], [6, 7]],
[[1, 2, 3, 4], [5, 6, 7]], [[1, 2, 3, 4, 5], [6, 7]]]
答案 1 :(得分:1)
修改强>
根据OP的新输入/输出要求,它只是几行:
def groupings(lst, min_size):
results = [tuple(lst)]
for i in range(min_size, len(lst)):
results.extend(combinations(lst, i))
return results
<强> ORIGINAL 强>
(基于错误的假设,即OP希望给定最小分区大小的所有可能分区。)
所以itertools.combinations()应该是一个起点。例如,
>>> list(combinations('ABCD', 2))
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
所以这给了你一个答案。您的分组输出&#39; ABCD&#39;最小尺寸设置为2是:
[['A', 'B', 'C', 'D']]
[['A', 'D'], ['B', 'C']]
[['A', 'C'], ['B', 'D']]
[['A', 'B'], ['C', 'D']]
所以高级流程应该是粗略的:
results = []
remaining = [([], list)] # Start without any groups and the full list
while remaining not empty:
groups, list = remaining.pop()
if len(list) >= min_size:
results.append(groups + list)
for size in min_size to len(list) - 1:
for combo in combinations:
new <- (groups + combo, list - combo)
if new will not cause duplicates:
remaining.append(new)
以下是一些似乎有用的代码。为了避免重复,并处理原始列表可能有重复的情况,我修改了itertools.combinations中的代码,而不是仅使用该方法。
def groupings(lst, min_size):
lst = list(lst)
# List for storing our final groupings
results = []
# Unfinished grouping, tuple storing the group and remaining sub-list
# Initialize with the empty group and original list
remaining = [([], lst)]
# Continue as long as we have unfinished groupings
while len(remaining):
# Get an unfinished grouping
current_group, current_lst = remaining.pop()
n = len(current_lst)
# If the last part of the list is big enough,
# then record the grouping
if n >= min_size:
results.append(current_group + [current_lst])
# Otherwise, discard it
else:
continue
# Helper set for getting remainder below
all_indices = set(range(n))
# Iterate the group length from min_size to the length of our current list
for r in range(min_size, n - 1):
# This is a modified version of itertools.combinations()
# http://docs.python.org/3.3/library/itertools.html#itertools.combinations
# Add the first combination to our remaining list
indices = list(range(r))
remainder = current_lst[r:]
group = current_group + [current_lst[:r]]
remaining.append((group, remainder))
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
break
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
# If one of the remaining indexes is less than the minimum used index,
# then a different iteration will handle it, so discard this one
min_index = min(indices)
remainder = []
for i in all_indices.difference(indices):
remainder.append(current_lst[i])
if i < min_index:
break
else:
# Add this combination to our remaining list
group = current_group + [[current_lst[i] for i in indices]]
remaining.append((group, remainder))
return results
结果:
>>> groupings('ABCDE', 2)
[['A', 'B', 'C', 'D', 'E']]
[['A', 'D', 'E'], ['B', 'C']]
[['A', 'C', 'E'], ['B', 'D']]
[['A', 'C', 'D'], ['B', 'E']]
[['A', 'B', 'E'], ['C', 'D']]
[['A', 'B', 'D'], ['C', 'E']]
[['A', 'B', 'C'], ['D', 'E']]
[['A', 'E'], ['B', 'C', 'D']]
[['A', 'D'], ['B', 'C', 'E']]
[['A', 'C'], ['B', 'D', 'E']]
[['A', 'B'], ['C', 'D', 'E']]