Python 用“最佳拟合”填充区域;积木;
我有一些代码可以根据用户的选择生成文本块。这些文本块的高度取决于用户选择的项目数量。我试图做的是确保这些块以最有效的方式排列在页面上 例如,截面1的高度为250点,截面2的高度为650点。如果用户选择: 表格a部分内容价值400点Python 用“最佳拟合”填充区域;积木;,python,algorithm,Python,Algorithm,我有一些代码可以根据用户的选择生成文本块。这些文本块的高度取决于用户选择的项目数量。我试图做的是确保这些块以最有效的方式排列在页面上 例如,截面1的高度为250点,截面2的高度为650点。如果用户选择: 表格a部分内容价值400点 b部分200点内容 c部分250点内容 d部分的50点内容 我如何确保b部分和d部分进入第1节,a部分和c部分进入第2节 以下是我目前的代码: section1_height = 250 section2_height = 650 #list1 and list2
b部分200点内容
c部分250点内容
d部分的50点内容 我如何确保b部分和d部分进入第1节,a部分和c部分进入第2节 以下是我目前的代码:
section1_height = 250
section2_height = 650
#list1 and list2 are the variables that contain the user selections
Column1 = DWIMBLOCK([list1], (18, 430), (LEFT, TOP_BASELINE), (250, 250))
Column2 = DWIMBLOCK([list2], (275, 430), (LEFT, TOP_BASELINE), (250, 650))
columns = [Column1, Column2]
sec1_columns = []
sec2_columns = []
for column in columns:
if column.height <= 250:
sec1_columns.append(column)
for shorts in sec1_columns:
if #This is where I am stuck
两幅图像始终位于同一位置,大小相同。还应该注意,这是用于PDF制作,而不是web使用,因此CSS和Javascript不是选项。我正在使用的环境只允许Python代码。基本上这是解决每个部分的背包问题(长度作为值和权重),一个接一个。我将使用蛮力来解决这个问题,但是你可以查找它并找到其他方法,这些方法在速度方面更有效,但可能不会给出最佳解决方案——这一种确实有效 有
2^b
(b
是块的数量)组合来填充第一部分,因为对于每个块,您可以将其放在那里,也可以不放在那里。其中只有一小部分是可行的。您可以选择填充最多的组合。然后,对下一节的其余项目重复此操作
这应该给你一个方法:
from itertools import combinations, chain
unassigned_blocks = {
('a', 400),
('b', 200),
('c', 250),
('d', 50),
# ...
}
sections_and_assigned_blocks = {
('1', 250): {},
('2', 650): {},
# ...
}
for section in sorted(sections_and_assigned_blocks.keys()):
best, best_length = {}, 0
for combination in chain(*[combinations(unassigned_blocks, n)
for n in xrange(1, len(unassigned_blocks)+1)]):
combination = set(combination)
length = sum(block[1] for block in combination)
if best_length < length <= section[1]:
best, best_length = combination, length
sections_and_assigned_blocks[section] = best
unassigned_blocks -= best
from pprint import pprint
pprint(sections_and_assigned_blocks)
# {('1', 250): set([('c', 250)]),
# ('2', 650): set([('a', 400), ('b', 200), ('d', 50)])}
来自itertools导入组合,链
未分配的块={
('a',400),
('b',200),
('c',250),
('d',50),
# ...
}
截面和分配的块={
('1', 250): {},
('2', 650): {},
# ...
}
对于已排序的节(节和分配的节块.keys()):
最佳,最佳_长度={},0
用于链中的组合(*[组合(未分配的块,n)
对于x范围内的n(1,len(未分配的_块)+1)]:
组合=集合(组合)
长度=总和(组合块中的块[1])
如果best_lengthfrom itertools import combinations, chain
unassigned_blocks = {
('a', 400),
('b', 200),
('c', 250),
('d', 50),
# ...
}
sections_and_assigned_blocks = {
('1', 250): {},
('2', 650): {},
# ...
}
for section in sorted(sections_and_assigned_blocks.keys()):
best, best_length = {}, 0
for combination in chain(*[combinations(unassigned_blocks, n)
for n in xrange(1, len(unassigned_blocks)+1)]):
combination = set(combination)
length = sum(block[1] for block in combination)
if best_length < length <= section[1]:
best, best_length = combination, length
sections_and_assigned_blocks[section] = best
unassigned_blocks -= best
from pprint import pprint
pprint(sections_and_assigned_blocks)
# {('1', 250): set([('c', 250)]),
# ('2', 650): set([('a', 400), ('b', 200), ('d', 50)])}