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

后端 未结 3 957
春和景丽
春和景丽 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条回答
  •  广开言路
    2021-01-24 13:47

    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]
    

提交回复
热议问题