This is a question about Python for Moon.

Asked 1 years ago, Updated 1 years ago, 55 views

def ndiv(l,n):
    return [l[i:i + n] for i in xrange(0, len(l) // n * n, n)] + [[e] for e in l[len(l) // n * n:]]

    print "INSERTION : {}".format(ndiv(insertion, i))
    print "INSERTION1 : {}".format(ndiv(insertion1, i))
    ds = [sum(sd) for sd in ndiv(insertion,i)]
    sa = [sum(aa) for aa in ndiv(insertion1,i)]
    ss = [x+y for x,y in zip(ds,sa)]
    for ada in ss:
        log2 = math.log(float(i + 1), 2)
        qq = 3 * ada * (log2)
        print "Each Insertion : {}".format(qq)

    print "JOINING : {}".format(ndiv(joining, i))
    print "JOINING1 : {}".format(ndiv(joining1, i))
    aa = [sum(ad) for ad in ndiv(joining,i)]
    bb = [sum(ac) for ac in ndiv(joining1,i)]
    cc = [x+y for x,y in zip(aa,bb)]
    for qwe in cc:
        log2 = math.log(float(i + 1), 2)
        ab = 4 * qwe * (log2)
        print "Each Insertion : {}".format(ab)

Hello, I want to take out the result value calculated through the for statement separately and add it (the result value of each for statement), but I'm asking you because I don't know what to do.

ex) compute = qq + ab 

python python-2.7

2022-09-21 16:09

2 Answers

The logic is the same, so separate it with a separate function

# Return all field values in ss, cc list
def sumOfList(v_list, multiply):
    result = []
    for item in v_list:
        log2 = math.log(float(i + 1), 2)
        qq = multiply * item * (log2)
        print "Each Insertion : {}".format(qq)
        result.append(qq)
    return sum(result)


def ndiv(l,n):
    return [l[i:i + n] for i in xrange(0, len(l) // n * n, n)] + [[e] for e in l[len(l) // n * n:]]

    print "INSERTION : {}".format(ndiv(insertion, i))
    print "INSERTION1 : {}".format(ndiv(insertion1, i))
    ds = [sum(sd) for sd in ndiv(insertion,i)]
    sa = [sum(aa) for aa in ndiv(insertion1,i)]
    ss = [x+y for x,y in zip(ds,sa)]
    # Sum is received using the function
    sumOfSs = sumOfList(ss, 3)

    print "JOINING : {}".format(ndiv(joining, i))
    print "JOINING1 : {}".format(ndiv(joining1, i))
    aa = [sum(ad) for ad in ndiv(joining,i)]
    bb = [sum(ac) for ac in ndiv(joining1,i)]
    cc = [x+y for x,y in zip(aa,bb)]
    # Sum is received using the function
    sumOfCc = sumOfList(cc, 4)
    # adding two results
    compute = sumOfSs + sumOfCc     



2022-09-21 16:09

Hm... aside from code errors

If you let me know the result you want

Assign the for statement in the same indent block declared.

for ada in ss:
    # ...
    # qq = 3 * ada * (log2) <= qq declaration and value assigned
    # ...

for qwe in cc:
    # ...
    # ab = 4 * qwe * (log2) <=ab declaration and value assigned
    # ...

compute = qq + ab 

It's not this question If there's a logic problem or an error, Please let me know the phenomenon and organize the code again and upload it.


2022-09-21 16:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.