Python 列表中浮点的成员资格

标签 python python-3.x precision

假设我有一个 形式的元组列表,其形式为 (x, y),其中包含 xy > float 。检查点 (v,w) 是否靠近(欧几里得距离小于某个 epsilon)到 points 中的某个点的最 Pythonic 方法是什么?

(当然,最简单的方法是使用 for 循环来查找。)

我主要想知道 python 3 的情况,但作为引用,python 2 的答案也很好。使用开源库的答案也是可以接受的。

最佳答案

就我个人而言,我会这样做:

#given GIVEN_POINT, MAX_DISTANCE and POINTS
from math import sqrt
print(list(map(lambda e: e[0],filter(lambda x: x[1]<=MAX_DISTANCE, map(lambda p: (p,sqrt((p[0]-GIVEN_POINT[0])**2+(p[1]-GIVEN_POINT[1])**2)),POINTS)))))

说明:

from math import sqrt    
#given GIVEN_POINT, MAX_DISTANCE and POINTS
distance=lambda p: sqrt((p[0]-GIVEN_POINT[0])**2+(p[1]-GIVEN_POINT[1])**2)
#(the Euclidean distance from p to GIVEN_POINT where P is a point like (x,y))
point_distance = lambda p: (p,distance(p))
#map store the distance along with each point
points_n_distances = map(point_distance,POINTS)
#Now we have something like [((x1,y1),d1),((x2,y2),d2)...] in a map
close = lambda p: p[1]<=MAX_DISTANCE
closer_points = filter(close,points_n_distances)
#We do a filter based on the distances

to_point = lambda p: p[0]
filtered=map(to_point,closer_points)
#we remove the extra bit of data
#now filtered contains all the (x,y) points which are maximum of a MAX_DISTANCE far from GIVEN_POINT

不要忘记filtered仍然是一个map!

关于Python 列表中浮点的成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873722/

相关文章:

java - 相当于java中php中的bcdiv

python - shell脚本执行失败如何实现重试机制?

python-3.x - 使用 Python 使用 Uno 访问 LibreOffice Calc

python - QTableView 选择改变

Matlab 给出了错误的答案

algorithm - 推荐系统——均值平均精度度量优化

python - 在数据帧python中按时间步长在开头添加一个值

python - 在 Windows 10 上检测 USB 输入设备的插入/移除

python - 应用引擎,Flask-Socketio 服务器 Cors_Allowed_Origins header 丢失

python-3.x - 如何获取 ImageDraw 对象数组?