1

I'm making a script which compares the distance between two addresses, those addresses are stored in two different .csv files together combined with itertools.product.

Comparing is an if condition which checks if addresses are 1km away from each other.

Every address from addresses1 needs to be compared with every address from addresses2.

If the address in addresses1 is 1km away from the address in addresses2 it needs to move on the next comparing. Because the lists are too long, it's wasting of time to wait to move on the next one. - And that's the problem.

I tried with continue but it's not working.

Let's say I have two lists of addresses:

addresses1 = ["Address1","Address2","Address3","Address4"]
addresses2 = ["compAddress1","compAddress2,"compAddress3","compAddress4","compAddress5"]

producted_list = list(itertools.product(addresses1, addresses2))

for a,b in producted_list:
    BLOCK OF CODE WITH GEOLOCATIONS

    if(distance == 1km):
        print("Addresses are within 1km from each other, move on next one")
        continue

Let's see how it's comparing:

a[0] -> b[0]
a[0] -> b[1]
a[0] -> b[2]
a[0] -> b[3]
a[0] -> b[4]

a[1] -> b[0]
a[1] -> b[1]
a[1] -> b[2]
a[1] -> b[3]
a[1] -> b[4]

a[2] -> b[0]
a[2] -> b[1]
a[2] -> b[2]
a[2] -> b[3]
a[2] -> b[4]
.
.
.

Let's say that a[1] is matched with b[0], I want to immediately move on the a[2] without continuing with a[1] comparing with the rest of b[]

I've tried to use the code from Eric https://stackoverflow.com/a/14829934/11417917

with

for a in b:
    def doWork():
        for c in d:
                if somecondition:
                    return # <continue the for a in b loop?>
    doWork()

But it's not working, it interrupts, either it needs changing.

Uki.
  • 251
  • 2
  • 6

2 Answers2

1

Just write two loops and break out of the inner loop to continue the outer loop:

addresses1 = ["Address1", "Address2", "Address3", "Address4"]
addresses2 = ["compAddress1", "compAddress2", "compAddress3", "compAddress4", "compAddress5"]

for a in addresses1:
    for b in addresses2:
        BLOCK OF CODE WITH GEOLOCATIONS

        if(distance == 1km):
            print("Addresses are within 1km from each other, move on next one")
            break
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
1

You missed the applicable solution from the duplicate you cited. You need to break the inner loop; this will naturally continue the outer loop:

for a in addresses_1:
    for b in addresses_2:
        if distance(a, b) <= 1.00:
            print(a, b, "are within 1 km")
            break
Prune
  • 76,765
  • 14
  • 60
  • 81