简体   繁体   中英

Append list within nested for-loop

I have defined some data, which appends to some lists. in order to do this, I need to put them into some sort of nested loop condition. First a range between 1-15 creates then rebarnumber calculates, within the loop, I set those conditions when n <= rebarnumber is matched, *do something* , then continue when n >= rebarnumber do something else . The problem is when above conditions fullfilled, I do except to get list with full length of range numbers.

But instead gets this result.

[49.0] 1
[49.0, 49.0] 2
[49.0, 49.0, 49.0] 3
[49.0, 49.0, 49.0, 49.0] 4
[49.0, 49.0, 49.0, 49.0, 49.0] 5
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0] 6
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0] 7
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0] 8
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 9
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 10
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 11
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 12
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 13
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 14
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 15

Desired result, (just shown last 2 lines of print)

[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0] 14
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0] 15

The code:

h = 300
cb = 35
ct = 35
ca = 35
b= 300
y = 12
d = h - cb
ds = ct
a = 25
yb = 8
rebarnumber = math.floor((b-(2*cb+2*yb+y))/a)
disc = []
dis = []
Asi = []
Asci = []

for n in range(1,16):
    if n <= rebarnumber+1:
        Asi.append(int(3.1416/4*(y)**2))
        dis.append( h - (cb + yb + y/2 ))
        Asci.append(int(3.1416/4*(y)**2))
        disc.append( ct + yb + y/2 )
        if n >= rebarnumber:
            Asi.append(int(3.1416/4*(y)**2))
            dis.append( h - (cb + yb + y/2 ) - ca)
            Asci.append(int(3.1416/4*(y)**2))
            disc.append( cb + yb + y/2 + ca)
    print(disc, n)

What's it I do wrong?! any help!

There are a couple of awkward things in your code. Let's clear those up first.

  1. You have obviously imported math since you are using math.floor . Why not also use math.pi instead of 3.1416? And, since it's a constant, why not make use of the standard PEP8 naming convention, ALL_CAPS? Also, why not use :

     import math # ... PI_Y2_4 = int((math.pi * y ** 2) / 4) 
  2. There are several "common subexpressions" that you could store as variables. Since, in your example code, they appear to be constants, you might even make them constants. But even if they aren't constant in your real code, you can gain clarity, avoid errors, and improve speed by computing them just once:

     for n in range(1, 16): cbyby_2 = cb + yb + y / 2 ctyby_2 = ct + yb + y / 2 if n <= rebarnumber+1: Asi.append(PI_Y2_4) Asci.append(PI_Y2_4) dis.append(h - cbyby_2) disc.append(ctyby_2) if n >= rebarnumber: Asi.append(PI_Y2_4) Asci.append(PI_Y2_4) dis.append(h - cbyby_2 - ca) disc.append(cbyby_2 + ca) 

    Also, are you sure about ct and cb when computing values for disc ? Everything else stays the same, but those two change. Could that be a typo?

Now, with those issues out of the way, let's look at your "got" versus "wanted" examples. You "got" a bunch of lists that stop growing at n == 9 . You "wanted" a bunch of lists that kept growing (by one) up to n == 15 .

Looking at the shape of your code, I see this:

for ...:
    if ...
        if ...
    print()

That says the print statement will occur every time, but the append methods will only be invoked when the first if statement passes. Furthermore, since the second if statement is contained inside the first if statement, when that statement passes you will append twice to each list.

Obviously, that's not what you want. Instead, I suspect you're trying to do a sort of if/else series, where one set of behavior pertains up to 8 or 9, and then another set of behavior takes over.

Since you mentioned else in a comment, I'm guessing this is a homework assignment or something where you haven't learned else yet.

In that case, you need to make sure your conditions are directly reversed from each other! The easiest way to do that is using not . (Note the two if statements are indented the same amount! The second if is not contained inside the first.):

for n in ...:
    ...
    low_rebar = (n <= rebarnumber + 1)

    if low_rebar:
        Asi.append(PI_Y2_4)
        Asci.append(PI_Y2_4)
        dis.append(h - cbyby_2)
        disc.append(ctyby_2)

    if not low_rebar:
        Asi.append(PI_Y2_4)
        Asci.append(PI_Y2_4)
        dis.append(h - cbyby_2 - ca)
        disc.append(cbyby_2 + ca)

    print(disc, n)

If you haven't used boolean expressions, you might not be comfortable storing the result in a variable. In that case you can spell it out:

    if n <= rebarnumber + 1:
        ...
    if not (n <= rebarnumber + 1):
        ...

Or you could mathematically reverse the condition:

    if n > rebarnumber + 1:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM