Skip to content
Advertisement

Print values of an array as x coordinates and y coordinates seperately – IndexError: list index out of range

I am trying to plot values of x coordinates and y coordinates of image array seperately

step1: 2 image arrays are given of size (10,2)

step2: find matching coordinates if they are same or distance between them is lessthan 3

step3: print x coordinates and y coordinates of image1 & image 2

My code:

def euclideanDistance(coordinate1, coordinate2):
    dist= pow(pow(coordinate1[0] - coordinate2[0], 2) + pow(coordinate1[1] - 
coordinate2[1], 2), .5)
    if dist <= 3:
        return (coordinate1, coordinate2)
    return False

first_cords= [[17,268],[17,396],[18,243],[18,548],[33,331],[47,27],[19,702,[45,484],[44,179],[46,89]]
second_cords= [[16, 484],[17, 398],[17, 640],[18, 331],[33,331],[47,27],[19,702,[45,484],[44,179],[46,89]]

new_x_coords1= []
new_y_coords1=[]
new_x_coords2 = []
new_y_coords2 = []
for f_cord in first_cords:
    for s_cord in second_cords:
        if euclideanDistance(f_cord,s_cord) is not False:
            new_x_coords1.append(f_cord[0])
            new_y_coords1.append(f_cord[1])
            new_x_coords2.append(s_cord[0])
            new_y_coords2.append(s_cord[1])

#printing x coordinates only
for i in new_x_coords1:
 print(new_x_coords1[i] )

expected output: xcoordinates now: IndexError: list index out of range

Advertisement

Answer

for i in new_x_coords1: print(new_x_coords1[i] ),i is the data in new_x_coords1,not index,if you want to use index:

for i in range(len(new_x_coords1)):
    print(new_x_coords1[i])

code:

def euclideanDistance(coordinate1, coordinate2):
    dist= pow(pow(coordinate1[0] - coordinate2[0], 2) + pow(coordinate1[1] - coordinate2[1], 2), .5)
    if dist <= 3:
        return (coordinate1, coordinate2)
    return False

first_cords = [[17,268],[17,396],[18,243],[18,548],[33,331],[47,27],[19,702],[45,484],[44,179],[46,89]]
second_cords = [[16, 484],[17, 398],[17, 640],[18, 331],[33,331],[47,27],[19,702],[45,484],[44,179],[46,89]]

new_x_coords1= []
new_y_coords1=[]
new_x_coords2 = []
new_y_coords2 = []
for f_cord in first_cords:
    for s_cord in second_cords:
        if euclideanDistance(f_cord,s_cord) is not False:
            new_x_coords1.append(f_cord[0])
            new_y_coords1.append(f_cord[1])
            new_x_coords2.append(s_cord[0])
            new_y_coords2.append(s_cord[1])

#printing x coordinates only
for i in new_x_coords1:
    print(i)

result:

17
33
47
19
45
44
46
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement