Home > Software engineering >  My for loop seems to be executing too many times. Help please! Thank you
My for loop seems to be executing too many times. Help please! Thank you

Time:06-15

I'm hoping someone can help me. I am checking if a set of coordinates I have are within a range of coordinates from a hurricane track. My dataset is quite large (over 10k) so I have just taken a small sample to show my code. When I run my for loop and if statement, the loop seems to iterating more times than it should (it seems to not move onto the next iterable). In the code the list of lists ida is my hurricane track coordinates and sample is my locations coordinates with an ID column.

ida = [['IDA', 18.6, -80.5],
 ['IDA', 18.619048, -80.571429],
 ['IDA', 18.638095, -80.642857],
 ['IDA', 18.657143, -80.714286],
 ['IDA', 18.67619, -80.785714],
 ['IDA', 18.695238, -80.857143],
 ['IDA', 18.714286, -80.928571],
 ['IDA', 18.733333, -81.0],
 ['IDA', 18.752381, -81.071429],
 ['IDA', 18.771429, -81.142857]]```

sample = ```[[25820, 25.891041, -80.383553], [25821, 34.016776, -117.837528], [25822, 33.812013, -117.886268], [25823, 47.980291, -122.207616], [25824, 32.553882, -116.978896], [25825, 34.102001, -118.324049], [25826, 33.410692, -112.418414], [25827, 51.503139, 5.477582], [25828, 52.248382, -0.810513], [99999, 18.6, -80.5]]```

Note: Last list in sample is self inputted to check that the loop is working (its the same point as a storm coordinate)

cargo[0][1] == the ID.

The loop:

ID_exposed = []
for loc in sample:
    for H in ida:
        if ((loc[1] > H[1]-0.6) and (loc[1] < H[1] 0.6)) and (loc[2] > H[2]-0.7) and (loc[2] < H[2] 0.7):
            ID_exposed.append(loc)```
print(ID_exposed)

Output:

[[99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5], [99999, 18.6, -80.5]]

Thanks community!

CodePudding user response:

It is a bit confusing, are you meaning to check every IDA element for every sample element. right now your loop should be running len(sample)*len(IDA) times. you can put a simple count inside the innermost loop to verify this.

CodePudding user response:

So I'm a bit confused as to what you're expected output it. What your code is doing right now, is iterating through your sample list, and for each iteration in sample it iterates through every item in ida. If a certain number of criteria pass, it adds that iteration of sample to your ID_exposed list. It continues doing this until you've run through every item in the sample list.

If you want it to stop after adding a certain number of locations to the list, you should add a counter and an If Statement with a break or continue statement in it:

ID_exposed.append(loc)
counter  = 1
if counter > 5:    #make it whatever number you want
    break          #the loop with break, and you will now exit your for loop

If you're looking for something else, you'll need to update your question to give us a better understanding of the problem.

CodePudding user response:

tl;dr: add a break line after your condition is met and loc is appended to ID_exposed. This won't solve your code from running too many times, but it will help stop the duplicate points from being added to your ID_exposed list.

Hello,

When I ran your code, it looks like it was appending the same data point over and over again. In the sample you gave, there is one ID that is meeting your condition. However, it is doing that exact comparison 10 times and appending it each time.

Therefore, I recommend adding a break each time your condition is met and the data was appended. This reduced the number of loops from 100 to 90, and the length of ID_exposed reduced from 10 to 1.

It looks like that for each sample point you are also iterating through the ida data. So the number of times it should loop would be equal to len(ida) * len (sample).In this example, both ida and sample have length 10, so it should loop 10 x 10 or 100 times. I ran this with a counter for each time it looped through the function and at the end the counter was 100, which would signal that the code is running the correct number of times.

Below is the sample that I used to validate it was running the correct number of times.

ID_exposed = []
ct = 0
for loc in sample:
    for H in ida:
        if ((loc[1] > H[1]-0.6) and (loc[1] < H[1] 0.6)) and (loc[2] > H[2]-0.7) and (loc[2] < H[2] 0.7):
            ID_exposed.append(loc)
        ct  = 1
print(ID_exposed)
print(ct)

This is the changed I made to make it run 90 times instead of 100

ID_exposed = []
ct = 0
for loc in sample:
    for H in ida:
        if ((loc[1] > H[1]-0.6) and (loc[1] < H[1] 0.6)) and (loc[2] > H[2]-0.7) and (loc[2] < H[2] 0.7):
            ID_exposed.append(loc)
            break
        ct  = 1
print(ID_exposed)
print(ct)
  • Related