简体   繁体   中英

Nested for loops only run once

I am attempting to run a nearest neighbor sort in python. I have a dataFrame full of points, example:

       x      y
1     10   10.0
2     26   11.0
3     27   20.0
4     36   19.0
...

up to 1000 points. I am trying to sort these points by shortest distance to any unused point in the dataFrame. The code I am currently using to do this sort is shown below.

for j in range(0, len(data)-2):
    minDist = 1000000
    k = j+1
    for i in range(k, len(data)-1):
        #dist1 = distance.euclidean(j, i+1)
        dist2 = distance.euclidean(j, i)

        if(dist2<minDist):
            minDist = dist2
            print(minDist)
            minI = data.iloc[i]

    b, c = data.iloc[j+1].copy(), data.iloc[i].copy()
    data.iloc[j+1],data.iloc[i] = c, b

However, when I run this code, my output data file only moves one data point, and it's not the correct data point, as shown here:

         x      y
1     10.0   10.0
2    624.0  436.0
3     26.0   11.0
4     27.0   20.0

I believe it is some problem with the nested for loops, however I am not sure. Are there any errors with my for loops? Or is it just a problem with how I'm approaching the problem in Python?

If you are trying to nest the for loops, you are doing it wrong, as the indentation used with the first for loop is incorrect. To nest them you would have to do something like this:

for j in range(0, len(data)-2):
    minDist = 1000000
    k = j+1
    for i in range(k, len(data)-1):
        #dist1 = distance.euclidean(j, i+1)
        dist2 = distance.euclidean(j, i)

        if(dist2<minDist):
            minDist = dist2
            print(minDist)
            minI = data.iloc[i]

    b, c = data.iloc[j+1].copy(), data.iloc[i].copy()
    data.iloc[j+1],data.iloc[i] = c, b

Solution to the issue:

Second loop was not iterating with respect to the first loop, so the "k=j+1" line was added.

Also added the minDist = 10000000, to ensure that the first comparison was correct and didn't skip an initial point.

for j in range(0, len(data)-1):
minDist = 1000000
k = j+1
for i in range(k, len(data)):
    #dist1 = distance.euclidean(j, i+1)
    dist2 = distance.euclidean(data.iloc[j], data.iloc[i])

    if(dist2<minDist):
        minDist = dist2
        #print(minDist)
        minI = i

b, c = data.iloc[j+1].copy(), data.iloc[minI].copy()
data.iloc[j+1],data.iloc[minI] = c, b

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