'Struggling to iterate through a list with a list

i am in need of help. The idea is to calculate distance between two points via lat and long. That being between customer's lat and long and the store's lat and long. Repeat this for other stores. This seems to me, i have to iterate through a list with another list. I have been struggling to implement this for several hours now.

EXCEL VIEW

# Backup.xlsx/AccountLocation.xlsx
    # column 0 = account numbers
    # column 1 = postcodes
    # Column 2 = latitude
    # Column 3 = longitude
    # Column 4 = order total (men)
    # Column 5 = order total (women)
    # Column 6 = order total (children)
# location.xlsx
    # Column 1 = City name
    # Column 2 = latitude
    # Column 3 = longitude
# Income.xlsx
    # Column 1 = City name
    # Column 2 = estimated income (Men)
    # Column 3 = estimated income (Woman)
    # Column 4 = estimated income (Children)

Code

import pandas as pd
import math
from openpyxl import load_workbook

def distance2Point(inputLat1, inputLon1, inputLat2, inputLon2):
    lat1 = float(inputLat1)
    lon1 = float(inputLon1)
    lat2 = float(inputLat2)
    lon2 = float(inputLon2)
    R = 6371 #metres
    radLat1 = lat1 * (numpy.pi)/180 #φ, λ in radians
    radLat2 = lat2 * (numpy.pi)/180
    diffLat = (lat2-lat1) * (numpy.pi)/180
    diffLong = (lon2-lon1) * (numpy.pi)/180
    a = numpy.sin(diffLat/2) * numpy.sin(diffLat/2) + numpy.cos(radLat1) * numpy.cos(radLat2) * numpy.sin(diffLong/2) * numpy.sin(diffLong/2)
    c = 2 * math.atan((math.sqrt(a))/(math.sqrt(1-a)))
    d = R * c #in metres
    return d

accountLocation = pd.read_excel("Backup.xlsx", header=None)

storeLocation = pd.read_excel("locations.xlsx", header=None)

accountLatitude = (accountLocation.iloc[:,2]).tolist()
accountLongitude = (accountLocation.iloc[:,3]).tolist()

storeLatitude = (storeLocation.iloc[:,1]).tolist()
storeLongitude = (storeLocation.iloc[:,2]).tolist()

londonDistance = []

for list in a:
    for number in list:
        print number

Edit: Sorry, i forgot to mention. Considering i had to use my own haversine formula. Which i got to work, but i am just simply struggling to iterate through this list.



Solution 1:[1]

There's a lot I would change about this code. For starters, do not provide lat and lon as separate variables. It's confusing. Instead use a tuple which is a standard way of dealing with coordinates.

Secondly, there's no reason to implement your own version of the haversine distance when reputable packages have it implemented already, for example, https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html.

So the code in the end could look something like:

from __future__ import annotations

from math import radians

from sklearn.metrics.pairwise import haversine_distances

def distance2point(point1: tuple[float, float], point2: tuple[float, float]):
    lats = [radians(point1[0]), radians(point2[0])]
    lons = [radians(point1[1]), radians(point2[1])]
    return haversine_distances(lats, lons)

or even simpler for multiple points:

def distance2point(points: list[tuple[float, float]]):
    lats = [radians(p[0]) for p in points]
    lons = [radians(p[1]) for p in points]
    return haversine_distances(lats, lons)

EDIT: Oopsie, forgot that sklearn expects radians. Fixed now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1