我有这个代码
from scipy.spatial import distance
mn= [(252, 468), (252, 495), (274, 481), (280, 458), (298, 479), (301, 458), (324, 499)]
name=['loc1','loc2','loc3','loc4','loc5','lco6','loc7']
zz= [(329, 478), (336, 455), (346, 499), (352, 478), (374, 468), (381, 499), (395, 459), (406, 488)]
L = pd.Series()
for name, i in list(zip(name, mn)):
for e in zz:
L[name] = distance.euclidean(e, i)
print(L)
w=100
dd = np.sqrt(np.power(L, 2) + np.power(w, 2))
print(dd)
这是它作为L&dd输出的结果:
loc1 155.293271
loc2 154.159009
loc3 132.185476
loc4 129.522199
loc5 108.374351
lco6 109.201648
loc7 82.734515
dtype: float64
loc1 184.705170
loc2 183.752551
loc3 165.749811
loc4 163.633737
loc5 147.461859
lco6 148.070929
loc7 129.788289
我的麻烦是它只给zz中的一个点提供L&dd,但是我想让zz中的每个点具有L,然后能够使用它为L中的每个值获取dd。
谢谢你的帮助!
你快到了 基本上,您可以使用以下代码替换for循环:
L = pd.Series()
for name, i in list(zip(name, mn)):
j = [] # save the intermediary results here
for e in zz:
j.append(distance.euclidean(e, i))
L[name] = j # append at once all computations are done
这会给你这样的东西:
loc1 [77.64663547121665, 85.0, 98.97979591815695, 1...
loc2 [78.85429601486528, 93.03762679690406, 94.0850...
loc3 [55.08175741568164, 67.23094525588644, 74.2159...
loc4 [52.92447448959697, 56.08029957123981, 77.6981...
loc5 [31.016124838541646, 44.94441010848846, 52.0, ...
lco6 [34.40930106817051, 35.12833614050059, 60.8769...
loc7 [21.587033144922902, 45.60701700396552, 22.0, ...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。