如何获得每个子集的最大尺寸限制列表的所有可能的设置分区?
这是我想做的。给定数字k和一组数字,我想用大小不大于k大的元素来划分集合。
ex)lst = [1,2,3,4],k = 2
LST的所有可能的设置分区如下。代码在下面的上一个问题中: (在Python中设置分区)
1 [[1, 2, 3, 4]]
2 [[1], [2, 3, 4]]
3 [[1, 2], [3, 4]]
4 [[1, 3, 4], [2]]
5 [[1], [2], [3, 4]]
6 [[1, 2, 3], [4]]
7 [[1, 4], [2, 3]]
8 [[1], [2, 3], [4]]
9 [[1, 3], [2, 4]]
10 [[1, 2, 4], [3]]
11 [[1], [2, 4], [3]]
12 [[1, 2], [3], [4]]
13 [[1, 3], [2], [4]]
14 [[1, 4], [2], [3]]
15 [[1], [2], [3], [4]]
但是,每个分区的大小不应超过k = 2。 任何子集具有超过k = 2个元素的所有情况均应删除。 因此,结果应该如下:
3 [[1, 2], [3, 4]]
5 [[1], [2], [3, 4]]
7 [[1, 4], [2, 3]]
8 [[1], [2, 3], [4]]
9 [[1, 3], [2, 4]]
11 [[1], [2, 4], [3]]
12 [[1, 2], [3], [4]]
13 [[1, 3], [2], [4]]
14 [[1, 4], [2], [3]]
15 [[1], [2], [3], [4]]
Python中会有算法吗?
Here is what I am trying to do. Given a number k and a set of numbers, I want to partition the set with elements of size not bigger than k.
Ex) lst = [1, 2, 3, 4], k=2
All possible set partition of lst is as follows. codes are in previous question below:
(Set partitions in Python)
1 [[1, 2, 3, 4]]
2 [[1], [2, 3, 4]]
3 [[1, 2], [3, 4]]
4 [[1, 3, 4], [2]]
5 [[1], [2], [3, 4]]
6 [[1, 2, 3], [4]]
7 [[1, 4], [2, 3]]
8 [[1], [2, 3], [4]]
9 [[1, 3], [2, 4]]
10 [[1, 2, 4], [3]]
11 [[1], [2, 4], [3]]
12 [[1, 2], [3], [4]]
13 [[1, 3], [2], [4]]
14 [[1, 4], [2], [3]]
15 [[1], [2], [3], [4]]
However, the size of each element of each partition should not exceed k = 2.
All the case where any subset has more than k=2 elements should be deleted.
Therefore, the result should be as follows:
3 [[1, 2], [3, 4]]
5 [[1], [2], [3, 4]]
7 [[1, 4], [2, 3]]
8 [[1], [2, 3], [4]]
9 [[1, 3], [2, 4]]
11 [[1], [2, 4], [3]]
12 [[1, 2], [3], [4]]
13 [[1, 3], [2], [4]]
14 [[1, 4], [2], [3]]
15 [[1], [2], [3], [4]]
Would there be an algorithm in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
过滤 link 您提供的答案的结果。例如,使用
more_itertools.set_partitions
如果要避免技术性(不在标准库中)。输出
Filter the result of an answer posted in the link you provided by length. For example with
more_itertools.set_partitions
if you want to avoid technicality (not in the standard library).Output