繁体   English   中英

检查python循环中的最小值

[英]Check minimum value in loop for python

我有这些数组:

A=np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) # points on x-axis
B=np.array([53.090028,  54.829213,  54.573222, 47.244701,  52.033966, 48.613694,  53.425587])  # points on y-axis

我创建(x,y)坐标数组:

coord = np.array([A, B]) 

我还有另一个坐标数组:

C=np.array([160.514, 161.67894, 161.68438, 160.59858,   161.55013, 161.61683, 161.55903, 161.67383, 161.70316,  161.63421 ])

D=np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067,  54.65673, 54.599929])

和一列距离:

z=np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])

现在,我想创建一个坐标的压缩数组,因此:

coord1=np.array([C,D])

目的是:搜索坐标中的点在坐标1中的位置,然后从z中提取相应的距离。 这是我的代码:

delta = 0.09
for i in range(coord.shape[1]):                                      
for j in range(coord1.shape[1]):
    if (np.all(coord[:,i] >= coord1[:,j]-delta)) and (np.all(coord[:,i] <= coord1[:,j]+delta)):  
       print coord[:,i], i, coord1[:,j], z[j], j

输出如下:

[ 160.592625   53.090028] 0 [ 160.514      53.068106] 0.000164209 0
[ 161.61683    54.829213] 1 [ 161.55013    54.852462] 0.303595 4
[ 161.61683    54.829213] 1 [ 161.61683    54.829213] 0.144146 5
[ 161.61683    54.829213] 1 [ 161.55903    54.916358] 0.142063 6
[ 161.61683    54.829213] 1 [ 161.67383    54.801067] 0.903051 7
[ 161.672708   54.573222] 2 [ 161.67894    54.552493] 0.0452326 1
[ 161.672708   54.573222] 2 [ 161.70316   54.65673] 0.144751 8
[ 161.672708   54.573222] 2 [ 161.63421    54.599929] 0.101169 9

如您所见,我与1和2具有多个对应关系。对于这些元素,我只想保留最小的z元素。 例如:在1中,我只想打印

[ 161.61683    54.829213] 1 [ 161.55013    54.852462] 0.303595 4

而且只有2秒

[ 161.672708   54.573222] 2 [ 161.67894    54.552493] 0.0452326 1

我不知道...先谢谢

我没有太多时间来优化此程序,但我希望这对您的数据量有效,因为它并不是很大-

您可以直接运行它,并检查finalResult是否给您期望的结果。 尝试了解算法,但它很重要。

import numpy as np

A = np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012])  # points on x-axis
B = np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587])  # points on y-axis

coord = np.array([A, B])

C = np.array(
    [160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421])

D = np.array(
    [53.068106, 54.552493, 53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])

z = np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])

coord1 = np.array([C, D])

delta = 0.09


# create a empty list and append whatever youre printing right now into it
mylst = list()
for i in range(coord.shape[1]):
    for j in range(coord1.shape[1]):
        if (np.all(coord[:, i] >= coord1[:, j] - delta)) and (np.all(coord[:, i] <= coord1[:, j] + delta)):
            temp = str(coord[:, i]) + " , " + str(i) + " , " + str(coord1[:, j]) + " , " + str(z[j]) + " , " + str(j)
            mylst.append(temp)

#see your list with all data
for each in mylst:
    print(each)

print("--------")

#create a new list for final output where you want records with min(Z)
finalResult = list()

for each in mylst:
    alpha = each
    # check if this element is already in final result
    alreadyDone = False
    for eachFR in finalResult:
        if str(alpha).split(",")[1] == str(eachFR).split(",")[1]:
            alreadyDone = True
            break
    if alreadyDone:
        continue

    # if not, look for minimum value of Z
    for ee in mylst:
        if str(alpha).split(",")[1] == str(ee).split(",")[1]:
            if str(alpha).split(",")[4] > str(ee).split(",")[4]:
                alpha = ee
    finalResult.append(alpha)

for each in finalResult:
    print(each)

不幸的是,麻木的专家还没有看过,所以您必须忍受我的尝试。 但是没有循环,只有numpy向量化函数。

import numpy as np

A=np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) 
B=np.array([53.090028,  54.829213,  54.573222, 47.244701,  52.033966, 48.613694,  53.425587]) 

C=np.array([160.514, 161.67894, 161.68438, 160.59858,   161.55013, 161.61683, 161.55903, 161.67383, 161.70316,  161.63421 ])
D=np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067,  54.65673, 54.599929])

z=np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
delta = 0.09

#calculate all differences between A and C
diff_AC = np.abs(A[:,None] - C[None,:])
#calculate all differences between B and D
diff_BD = np.abs(B[:,None] - D[None,:]) 
#find pairs, where both coordinates differ by less than delta      
diff_coord = np.argwhere(np.logical_and(diff_AC < delta, diff_BD < delta))
#sort according to AB index and, if more than one exists, by the z value derived from CD index
diff_coord_sorted = diff_coord[np.lexsort((z[diff_coord[:,1]], diff_coord[:,0]))]
#find the first occurrence of each value in AB
_unik, unikindices = np.unique(diff_coord_sorted[:,0], return_index = True)
#get index for AB and CD
pairs_AB_CD = diff_coord_sorted[unikindices]

输出:

>>>[[0 0]
    [1 4]
    [2 9]]

每对代表一对A / B和C / D坐标的索引,它们之间的距离小于delta,且z值最小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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