Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问嵌套索引?_Python_Python 3.x_List - Fatal编程技术网

Python 如何访问嵌套索引?

Python 如何访问嵌套索引?,python,python-3.x,list,Python,Python 3.x,List,我想用欧几里得公式。我需要x2和y2分别作为嵌套在蓝色_点中的所有列表的内部[1]和[2]索引,但它不这样做。我试图用point1列表计算蓝_点的欧几里德距离,然后确定它是否小于或大于/等于,这样程序可以分别返回True或False import math # [ID, X coordinate, Y coordinate] blue_points = [[30, 536254.99137, 3659453.06343], [33, 536721.584912, 3659162.97207],

我想用欧几里得公式。我需要x2和y2分别作为嵌套在蓝色_点中的所有列表的内部[1]和[2]索引,但它不这样做。我试图用point1列表计算蓝_点的欧几里德距离,然后确定它是否小于或大于/等于,这样程序可以分别返回True或False

import math

# [ID, X coordinate, Y coordinate]
blue_points = [[30, 536254.99137, 3659453.06343],
[33, 536721.584912, 3659162.97207],
[50, 535807.099324, 3659576.92825],
[112, 536827.131371, 3657913.01245],
[117, 536473.254082, 3659433.57702],
[120, 536196.9844, 3658713.72722],
[127, 536387.547701, 3658527.70015],
[133, 537397.838429, 3659554.48657],
[144, 537715.931243, 3658625.59997],
[164, 538367.648437, 3658867.34288],
[172, 537112.662366, 3657921.28957],
[173, 536418.315024, 3658715.47946],
[209, 538096.28422, 3658514.93514],
[211, 538077.87716, 3658138.39337],
[223, 536220.396985, 3659243.54161],
[242, 536102.087002, 3658703.61054],
[244, 536968.755886, 3659409.42857],
[246, 535996.903591, 3658705.08691],
[275, 538078.165429, 3659022.35547],
[303, 535999.885405, 3658521.91524]]

# [ID, X coordinate,Y coordinate]
point1 = [1, 1073706.744,3658967.925]

neighbor_points = [] # Empty list to store values.
for point2 in blue_points:
    if (math.sqrt(((point2[1] - point1[1]) ** 2) + ((point2[2] - point1[2]) ** 2))): # Euclidean distance formula.
        neighbor_points.append(point2) # Add points to empty list.
        print(point2[1:3]) # Print to check if indeces are correct.

x1 = point1[1]
y1 = point1[2]
x2 = blue_points[1]
y2 = blue_points[2]
                
distance = math.sqrt((neighbor_points[1] - point1[1]) ** 2) + ((neighbor_points[2] - point1[2]) ** 2)

def identify_neighbor(point1, point2):
    
    if (distance < 536000):
        print(distance)
        print("True.")
        return True
    else:
        print(distance)
        print("False")
        return False

neighbors = []
for point in blue_points:
    if (math.sqrt(((neighbor_points[1] - point1[1]) ** 2) + ((neighbor_points[2] - point1[2]) ** 2))):
        neighbors.append(point)
        print(point[0])
        identify_neighbor(point1, point2)
导入数学
#[ID,X坐标,Y坐标]
蓝点=[[30,536254.99137,3659453.06343],
[33, 536721.584912, 3659162.97207],
[50, 535807.099324, 3659576.92825],
[112, 536827.131371, 3657913.01245],
[117, 536473.254082, 3659433.57702],
[120, 536196.9844, 3658713.72722],
[127, 536387.547701, 3658527.70015],
[133, 537397.838429, 3659554.48657],
[144, 537715.931243, 3658625.59997],
[164, 538367.648437, 3658867.34288],
[172, 537112.662366, 3657921.28957],
[173, 536418.315024, 3658715.47946],
[209, 538096.28422, 3658514.93514],
[211, 538077.87716, 3658138.39337],
[223, 536220.396985, 3659243.54161],
[242, 536102.087002, 3658703.61054],
[244, 536968.755886, 3659409.42857],
[246, 535996.903591, 3658705.08691],
[275, 538078.165429, 3659022.35547],
[303, 535999.885405, 3658521.91524]]
#[ID,X坐标,Y坐标]
点1=[11073706.7443658967.925]
邻居点=[]#存储值的空列表。
对于蓝色点中的点2:
如果(math.sqrt(((point2[1]-point1[1])**2)+((point2[2]-point1[2])**2)):#欧氏距离公式。
相邻点。追加(点2)#将点添加到空列表。
打印(第2点[1:3])#打印以检查索引是否正确。
x1=点1[1]
y1=点1[2]
x2=蓝色_点[1]
y2=蓝色_点[2]
距离=数学sqrt((相邻_点[1]-点1[1])**2+((相邻_点[2]-点1[2])**2)
def标识_邻居(点1、点2):
如果(距离<536000):
打印(距离)
打印(“真”)
返回真值
其他:
打印(距离)
打印(“假”)
返回错误
邻居=[]
对于蓝色点中的点:
如果(math.sqrt(((相邻_点[1]-点1[1])**2)+(相邻_点[2]-点1[2])**2)):
邻居。追加(点)
打印(点[0])
识别邻居(点1、点2)

好吧,你的密码很奇怪。但只有一个问题

要使用
python数组
访问嵌套数组(矩阵),您需要
mat[i][j]
访问de元素
i,j

因此,对于代码运行,您只需要更改距离函数

distance=math.sqrt((相邻_点[1]-点1[1])**2+((相邻_点[2]-点1[2])**2)

distance=math.sqrt((相邻_点[0][1]-点1[1])**2+((相邻_点[0][2]-点1[2])**2)
对于进一步的实现,我建议您创建一个这样做的函数

def欧几里得距离(p1,p2):
返回math.sqrt((p2[1]-p1[1])**2+((p2[2]-p1[2])**2)
你可以这样称呼它

距离=欧几里德距离(相邻点[0],点1)
有关更多信息,请参见或

如果我正确理解了目标,您希望在
蓝色\u点
中找到与
点1
相邻的所有点。为此,您可以循环通过
蓝色_点
,找到每个点的距离,然后在列表中收集相邻点

请尝试以下代码:

import math

# [ID, X coordinate, Y coordinate]
blue_points = [
[30, 536254.99137, 3659453.06343],
[33, 536721.584912, 3659162.97207],
[50, 535807.099324, 3659576.92825],
[112, 536827.131371, 3657913.01245],
[117, 536473.254082, 3659433.57702],
[120, 536196.9844, 3658713.72722],
[127, 536387.547701, 3658527.70015],
[133, 537397.838429, 3659554.48657],
[144, 537715.931243, 3658625.59997],
[164, 538367.648437, 3658867.34288],
[172, 537112.662366, 3657921.28957],
[173, 536418.315024, 3658715.47946],
[209, 538096.28422, 3658514.93514],
[211, 538077.87716, 3658138.39337],
[223, 536220.396985, 3659243.54161],
[242, 536102.087002, 3658703.61054],
[244, 536968.755886, 3659409.42857],
[246, 535996.903591, 3658705.08691],
[275, 538078.165429, 3659022.35547],
[303, 535999.885405, 3658521.91524]]

# [ID, X coordinate,Y coordinate]
point1 = [1, 1073706.744,3658967.925]

neighbor_points = []
outer_points = []  # not neighbor

for pt in blue_points:
   distance = math.sqrt(((pt[1] - point1[1]) ** 2) + ((pt[2] - point1[2]) ** 2))
   if (distance < 536000):
        neighbor_points.append(pt)
   else:
        outer_points.append(pt)

print('neighbor_points')
for pt in neighbor_points:
   print(pt)
print()
print('outer_points')
for pt in outer_points:
   print(pt)

这回答了你的问题吗?
neighbor_points
[144, 537715.931243, 3658625.59997]
[164, 538367.648437, 3658867.34288]
[209, 538096.28422, 3658514.93514]
[211, 538077.87716, 3658138.39337]
[275, 538078.165429, 3659022.35547]

outer_points
[30, 536254.99137, 3659453.06343]
[33, 536721.584912, 3659162.97207]
[50, 535807.099324, 3659576.92825]
[112, 536827.131371, 3657913.01245]
[117, 536473.254082, 3659433.57702]
[120, 536196.9844, 3658713.72722]
[127, 536387.547701, 3658527.70015]
[133, 537397.838429, 3659554.48657]
[172, 537112.662366, 3657921.28957]
[173, 536418.315024, 3658715.47946]
[223, 536220.396985, 3659243.54161]
[242, 536102.087002, 3658703.61054]
[244, 536968.755886, 3659409.42857]
[246, 535996.903591, 3658705.08691]
[303, 535999.885405, 3658521.91524]