检查python循环中的最小值
Posted
技术标签:
【中文标题】检查python循环中的最小值【英文标题】:Check minimum value in loop for python 【发布时间】:2018-07-09 09:23:39 【问题描述】:我有这些数组:
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])
目的如下:搜索coord中的点在coord1中的位置,然后从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元素。例如:1s中,我只想打印
[ 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
我不知道...提前谢谢
【问题讨论】:
伪代码-无论您要打印什么,将其放入列表中-然后遍历列表,将“i”作为索引,当您获得 Z 的最小值时-仅从列表中获取该值 谢谢!但通过这种方式,我将得到一个 z 值。我想捕获链接到索引 1 的最小值,索引 2 的最小值... 好吧,让我开发一些代码,你能告诉我这些数据有多大吗? 谢谢! coord 大约是 100,coord1 大约是 10000... 【参考方案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)
【讨论】:
谢谢,这是我所期望的解决方案,我的错误在循环中。成功了,谢谢^^【参考方案2】:很遗憾,numpy 专家还没有看到这个,所以你不得不忍受我的尝试。但没有循环,只有 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 值最小。
【讨论】:
谢谢,这是我没想到的一种方式,但在这种情况下,我必须使用循环。另一方面,我会记住您的解决方案 您可能希望使用较小的数据集来计时。 Numpy 函数是矢量化的,这意味着它们的执行速度比循环快得多。祝你的项目好运。以上是关于检查python循环中的最小值的主要内容,如果未能解决你的问题,请参考以下文章
python循环输入若干个同学的成绩,求出这些同学的成绩平均值、最小值和最大值?