简体   繁体   中英

How to structure the for loop to give me the correct output? List generated is longer than it should be

I have two lists ( tb_shade and tb_sun ) that I'm trying to iterate through and append a 1 to a third list ( activity_status ) if a criterion is met or a 0 if not. The tb_shade and tb_sun lists are the same length (8760). And I'm trying to create a third list ( activity_status ) that is also the same length, and that has a value of 0 if either of the values of tb_shade or tb_sun lists is outside of the numeric range: 29-39, or an 1 if either values are within that range. I've tried it a couple different ways. I've included two of those attempts below. Neither one works.

The first try appends 0 for all rows in the list even though I know the criteria are met at some times. The second try has an output activity_status list that is 122268322 values long. It should be the same as the tb_sun and tb_shade lists which are both 8760. Does anyone know how to get this to run? Any helps or tip would be appreciated! Thanks!

First try

activity_status=[]    
for i,q in zip(tb_shade, tb_sun):
    if tb_shade[i] > (39) or tb_sun[q] < (29):
        activity_status.append(0)
    else:
        activity_status.append(1)
        

Second try

    
activity_status=[]    
for i in range(len(tb_shade)):
    for q in range(len(tb_sun)):
        if tb_shade[i] > 39 or tb_sun[q] < 29:
            activity_status.append(0)
        if tb_shade[i] < 39 or tb_sun[q] > 29:
            activity_status.append(1)
print(len(activity_status)) #122268322
print(len(tb_shade)) #8760 

Your second try is almost correct. You have nested for loops so what you're actually doing is looping over tb_sun 8760 times and comparing every value in each array with each other. Try this:

activity_status=[]    
for i in range(len(tb_shade)):
    
    if tb_shade[i] > 39 or tb_sun[i] < 29:
        activity_status.append(0)
    if tb_shade[i] < 39 or tb_sun[i] > 29:
        activity_status.append(1)

print(len(activity_status))
print(len(tb_shade))

You are using an iteration with in an iteration and this is giving you the square of the result you are expecting. ie instead of counting just from 0 to 8759, it is looping from 0 to 8759 for 8760 times.

Instead, you can use one for loop and use that for iterating both lists.

Second, your criteria is not mutually exclusive from what you have written. For example, if the two numbers from tb_shade and tb_sun are 30 and 50 respectively, both if clauses are met and they both will execute. Assuming you are trying to check if the numbers in both cases is between 29 and 39, I have made some changes to your code.

activity_status=[]    
for i in range(len(tb_shade)):
    if tb_shade[i]>29 and tb_shade[i]<39 and tb_sun[i]>29 and tb_sun[i]<39:
        activity_status.append(1)
    else:
        activity_status.append(0)

print(len(activity_status)) #should give 8760 if both lists are of length 8760
print(len(tb_shade)) #8760 

Using list comprehension, the item is 1 if the values of in table tb_shade and tb_sun are within the range [29, 39]. Otherwise, the item is 0.

activity_status = [1 if (29 <= shade <= 39 and 29 <= sun <= 39) else 0 for shade, sun in zip(tb_shade, tb_sun)]

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