有没有办法生成一个集合的所有可能的子集?

问题描述

** 我将在开头说,我很有可能以错误的方式处理整件事! **

也就是说....我正在尝试构建一个程序,该程序将从给定数量的输入长度中最大限度地减少浪费。

该场景是输入您需要的木材长度,然后程序会将它们与木材的木材长度进行比较,并返回您应该购买的木材长度的最佳组合。出于这个问题的目的,我只是将输入长度放入代码中。

我目前正在制定一个包含这组数字的所有可能排列的列表:

import itertools
from itertools import combinations,permutations


standard_lengths = [2400,3000,3600,4000]
input_lengths = []
optimum_scenario_waste = 0
optimum_scenario_cut_list = []

inputs = [2700,3500,1200,1900,200]

order_combos = (list(permutations(inputs,len(inputs))))

然后我正在尝试编写一段代码,该代码将为我提供这些变体的所有可能子集

即对于排列 [3500,200,2700]

我希望得到:[(3500),(1900),(1200),(200),(2700)] [(3500,1900),(2700)] [(3500,(1200,200),(2700)] 等等....

在这里不明白的主要问题是如何创建不同数量的子集(或等于 0 的子集,即如果 len(inputs) = 5 = 子集数,如何在某些实例,然后是具有不同元素数量的子集)。

这是我目前所做的,我知道它是错误的并且不起作用,并且只是输入 5 的近似值。

sum_combos = []

for ordered_list in order_combos:
    for x in range(len(ordered_list)):
        first_combo = ordered_list[:x+1]
        second_combo = ordered_list[x+1:-1]
        third_combo = [ordered_list[-1]]
        if len(second_combo) > 0:
            sum_combos.append([first_combo,second_combo,third_combo])
  1. 然后我计算每个子集(或“木材组合”)的浪费,然后对输入的每个排列的总浪费求和。然后,我将其与其他排列进行比较,然后返回对应于具有最低浪费的子集排列的标准长度组合。

我知道这最后一点很乱,但它似乎有效

非常感谢!

combo_sum_totals = []

for combo in sum_combos:
    individual_combo_sum_total = []
    for sum_term in combo:
        sum_of_constituents = 0
        for x in sum_term:
            sum_of_constituents += x
        individual_combo_sum_total.append(sum_of_constituents)
    combo_sum_totals.append(individual_combo_sum_total)

    
def addition(summed_length):
    for i in range(len(standard_lengths)):
        if summed_length > 4000:
            waste = 99999999
            optimum_length = 0
            return [waste,optimum_length]
        elif standard_lengths[i] >= summed_length:
            waste = standard_lengths[i] - summed_length
            optimum_length = (standard_lengths[i])
            return [waste,optimum_length]
            break
        else:
            pass


waste_lengths_list = []

for sum_terms in combo_sum_totals:
    waste_lengths_list_individual = []
    for sum_term in sum_terms:
        waste,optimum_length = addition(sum_term)
        waste_lengths_list_individual.append([sum_term,waste,optimum_length])
    waste_lengths_list.append(waste_lengths_list_individual)

lowest_waste = 9999999
best_option = 0

for individual_lengths in waste_lengths_list:
    individual_waste_total = 0
    for individual_waste in individual_lengths:
        individual_waste_total += individual_waste[1]
    print(individual_waste_total)
    if individual_waste_total < lowest_waste:
        lowest_waste = individual_waste_total
        best_option = individual_lengths


print(best_option)
print(lowest_waste)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...