python - Output length mismatch in nested loops? -


i got problem number of outputs loop.

neighbours=[]#this array hold distance k-th neighbour  in range(0, len(selection)-1):#208 values in selection        n2 =[]#this array hold distance other 207 points       k in range(0, len(selection)-1):            d = {}           if != k:#to remove same point being considered               ra_diff = selection[i]['ra']-selection[k]['ra']               dec_diff= selection[i]['dec']-selection[k]['dec']               d=float(math.hypot(ra_diff , dec_diff))#finds distance               n2.append(d)         n2.sort()       neighbours.append(n2[6])#passes 7th value array 

this part of code find k-nearest neighbour. selection has 208 values, nested loop should calculate distance points , find 7th closest point each point.

after iteration neighbours array holds 207 values(i.e. len(neighbours)=207), there should 7th closest neighbour 208 values. can please point me problem is?

this line:

for in range(0, len(selection)-1): 

and

for k in range(0, len(selection)-1):  

are problems, range exclusive of stop parameter - 1 missing last element.

eg.

>>> l = [1, 2, 3] >>> range(len(l)) # goes 0 n - 1, n len(l) [0, 1, 2] 

but

>>> range(len(l) - 1) # goes 0 n - 2 [0, 1] 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -