How to alter this code to allow appending to the list?

后端 未结 3 959
春和景丽
春和景丽 2021-01-24 13:10

I have an issue appending or in fact printing anything after this block of code:

reversedPriv = [52,27,13,6,3,2]
array= [9]
var = 0
numA = []
for i in array:
            


        
3条回答
  •  -上瘾入骨i
    2021-01-24 14:10

    So in general, it's a good idea on SO to be clear about just what the question is, but it's often better to provide the context of your question.

    What you are working on is a fragment of a knapsack solver. As mentioned in my comments below, you may do better to just use or-tools out of the box as follows (taken from https://developers.google.com/optimization/bin/knapsack):

    from ortools.algorithms.pywrapknapsack_solver import KnapsackSolver
    
    def knapsack():
        solver = KnapsackSolver(
           KnapsackSolver.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
          'KnapsackExample'
        )
        weights = [[52,27,13,6,3,2]]
        capacities = [9]
    
        solver.Init(weights[0], weights, capacities)
        computed_value = solver.Solve()
    
        packed_items = []
        packed_weights = []
        total_weight = 0
        print('Total value =', computed_value)
        for i in range(len(weights[0])):
            if solver.BestSolutionContains(i):
                packed_items.append(i)
                packed_weights.append(weights[0][i])
                total_weight += weights[0][i]
    
        print('Total weight:', total_weight)
        print('Packed items:', packed_items)
        print('Packed_weights:', packed_weights)
    
    knapsack()
    

    Console:

    Total value = 9
    Total weight: 9
    Packed items: [3, 4]
    Packed_weights: [6, 3]
    

提交回复
热议问题