检查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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章