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

大憨熊 提交于 2019-12-20 05:31:58

问题


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:
    for j in reversedPriv:
        while var!= j:
            if j < i:
                var = var + j
                numA.append(j)
                numA.sort()
print(numA)

I am expecting it to append [3,6] to numA and print but it currently does nothing. Is there some condition for the while loop that I'm overlooking?

The point of the code is to find which elements in 'reversedPriv' sum up to each element in 'array' and append them to the list 'numA'. For example from the list of 'reversedPriv' only 6 and 3 sum up to 9. So numA = [3,6] Currently 'array' only has a single element but the code should be able to scale it up for n number of elements.


回答1:


Instead of j it should be var != i:

reversedPriv = [52,27,13,6,3,2]
array= [9]
var = 0
numA = []
for i in array:
    for j in reversedPriv:
        while var!= i:
            if j < i:
                var = var + j
                numA.append(j)
                numA.sort()
                break
            break
print(numA)

Or without the while,

reversedPriv = [52,27,13,6,3,2]
array= [9]
var = 0
numA = []
for i in array:
    for j in reversedPriv:
        if (var != i) and j < i:
            var = var + j
            numA.append(j)
numA.sort()
print(numA)

Or,

reversedPriv = [52,27,13,6,3,2]
array= [9]
var = 0
numA = []
numA = [sorted([x,y]) for x,y in zip(reversedPriv[:-1],reversedPriv[1:]) if x+y == array[0]][0]
print(numA)

Output:

[3, 6]



回答2:


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]



回答3:


I am not sure what are you trying to do, but it'seems you want 6 + 3 = 9, append 6,3 to numA

for it1 in reversedPriv:
    for it2 in reversedPriv:

        sumit = it1 + it2     # sum 1st iteraction with 2nd iteraction

        if sumit == array[0]: #extract array value from list

            if it1 not in numA and it2 not in numA:
                numA.append(it1)
                numA.append(it2)

print(numA)
#[6, 3]


来源:https://stackoverflow.com/questions/59170617/how-to-alter-this-code-to-allow-appending-to-the-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!