如何获得带有变量和约束的所有变体/组合? Python 3.6

问题描述 投票:0回答:1

我需要运筹学一个项目的帮助,因为我需要以下所有组合/变化:

我有一个轮廓的四个不同长度,长度为2.8m; 3.2m; 3.8m; 4.2m;并且它们的组合给出了我的总长度->一种组合,例如本示例中1x 2.8m和1x 3.2m以及1x 3.8m和0x 4.2m给出了11.2m

现在,我需要这四个长度的所有可能组合,但要限制为最大总长度为12m,最小为9.2m

如何在Python 3.6中做到这一点,这可能是解决方案?

预先感谢!

python-3.x constraints combinations variations
1个回答
0
投票

这应该工作

combinations = [(a * 2.8, b * 3.2, c * 3.8, d * 4.2) 
                for a in range(2) 
                for b in range(2) 
                for c in range(2) 
                for d in range(2)]
matches = [x for x in combinations if 9.2 <= sum(x) <= 12]
© www.soinside.com 2019 - 2024. All rights reserved.