在元组坐标上嵌套 for 循环

Nested for loop on tupled coordinates

我需要编写一个代码,最终将读取存储在 excel 表中的多个大数据表的坐标,但首先我想学习编写一个嵌套的 for 循环来分析中的元组下面的代码。

我能找到的所有嵌套 for 循环都没有这样的东西,所以我认为 post 在这里可能会很好。

我需要这段代码具体做的是获取file1中的第一个坐标并将其与file2中的每个坐标进行比较,然后将file1中的第二个坐标与file2中的每个坐标进行比较,依此类推循环遍历file2中的每个坐标file1 与文件中的每个坐标进行比较,然后 return 如果两者共享指定的接近度。

import math
file1 = ('1.36, 7.11', '1.38, 7.12', '1.5, -7.14', '8.7, 3.33', '8.5, 3.34', '8.63, -3.36')
file2 = ('1.46, 7.31', '1.47, 7.32', '1.49, -7.34', '8.56, 3.13', '8.57, 3.14', '8.59, -3.16')
dist = file1.apply(lambda row: math.hypot(row['x_diff'], row['y_diff']), axis=1)
for dist in file1:
    for dist in file2:
        if dist.values >= .5:
            print 'no match'
        elif dist.values <= .5:
            print True, dist

我的直觉是我没有填写适当的命令来读取元组作为坐标。此外,对于我应该在此处的声明中写什么,我感到很困惑 for dist in file1。我的意思是我应该调用什么以及如何适当地标记它。

我意识到这可能是一团糟,但是,这是我有史以来的第一个编码项目,所以如果绝对有人可以帮助引导我朝着正确的方向前进,或者提供一些关于我可能需要在这里更好地理解的内容的反馈,我将不胜感激欣赏它。

一般的 For 循环 :
您选择一个变量作为迭代器(可以是任何东西,但不应该是同时在其他地方使用的东西)迭代可迭代对象(例如列表)。在我下面的示例中,i 和 j 是迭代器,而 range(10) 是它们迭代的对象。在循环中,您可以写下所有想要重复的内容。在下面的示例中,我将每个可能的 i/j 组合附加到列表中。

嵌套 for 循环要求您使用两个不同的变量

示例:

whatever = []
for j in range(10):
    for i in range(10):
        whatever.append([j, i])

在 运行 代码之后,看起来像什么:

[ [0, 0], [0, 1], [0,2] ,... [1, 0], [1, 1], ... [9, 9] ]

假设您以元组的形式获取数据:

# convert file1 and file2 to lists of 2d points
# this is quite sloppy and I'll tidy it up when I get home from work
xs = [[float(pq.split(',')[0]),float(pq.split(',')[1])] for pq in list(file1)]
ys = [[float(pq.split(',')[0]),float(pq.split(',')[1])] for pq in list(file2)]
# generate a cartesian product of the two lists
cp = [(x,y) for x in xs for y in ys]
# generate distances
dists = map(lambda (x,y):math.hypot(x[0]-y[0],x[1]-y[1]),cp)
# loop through and find distances below some_threshold
for i in range(len(xs)):
    for j in range(1,len(ys)+1):
        if dists[i*j] > some_threshold:
            print i,j,dist
        else:
            print 'no match'

不过,如果您要阅读任何大小合理的数据集,我建议您使用 pandas 或 numpy。

您将元组表示为字符串,使用起来非常不方便。 "Real" 通常最好从元组开始。

file1 = [(1.36, 7.11), (1.38, 7.12), (1.5, -7.14), (8.7, 3.33)]
file2 = [(1.46, 7.31), (1.47, 7.32), (1.49, -7.34), (8.56, 3.13)]

下一个问题是,我们如何得到这两个xy点之间的距离? 为此,我们可以使用 scipy.spatial.distance.euclidean 作为一个函数,它接受两个元组和 returns 之间向量的欧几里德范数。例如:

> import scipy.spatial.distance as distance
> distance.euclidean(file1[0], file2[0])
0.22360679774997827

现在,我们来到您问题的核心:嵌套循环。 逻辑如下。对于 file1 中的每个元素,比如 coord1,我们取 file2 中的每个元素,比如 coord2 并计算 coord1coord2 之间的距离.

for coord1 in file1:
    for coord2 in file2:
        dist = distance.euclidean(coord1, coord2) # do not forget to import distance before
        if dist < 0.5:
            print True, dist
        else:
            print 'no match'

我会根据变量所代表的含义来命名变量。 file1 是一个 coordinate_list (first_coordinate_list, coordinate_list_1) 并且元素是坐标,例如coordinate, coordinate_1, left_coordinate.