2
import itertools

A = [50, 60, 70]
B = [0.3, 0.1, 0.5, 0.4]

print [a + b for a, b in itertools.product(A, B)]

>> [50.3,50.1,50.5,50.4,60.3,60.1,60.5,60.4,70.3,70.1,70.5,70.4]

In the above code, is there a way to just return the minimum value of a + b for each a? so the output should be:

[50.1,60.1,70.1]

please note this is a toy example, in the real example A and B are lists of lat and lon values and I compute the great circle distance between them

user308827
  • 21,227
  • 87
  • 254
  • 417
  • 3
    So why not just use `min_b = min(B)` then `[a + min_b for a in A]`? Why do N * M steps when N + M steps will suffice? – Martijn Pieters Apr 11 '15 at 14:08
  • this is a toy example, in the real example A and B are lat and lon values and I compute the great circle distance between them. updated question – user308827 Apr 11 '15 at 14:10
  • What is `great circle distance`? – thefourtheye Apr 11 '15 at 14:12
  • great circle distance is the distance between any two points on a globe like earth. here's an example: http://gis.stackexchange.com/questions/119846/calculating-distance-between-latitude-and-longitude-by-using-python. you could assume simple euclidean distance as well. – user308827 Apr 11 '15 at 14:13
  • FWIW, if you want more accuracy than the great circle distance formula, take a look at [Vincenty's formulae](http://en.wikipedia.org/wiki/Vincenty%27s_formulae), which treat the Earth as an ellipsoid rather than a sphere. It's quite a bit more complicated than Great Circle, so you may not want to use Vincenty unless you _really_ need it. :) – PM 2Ring Apr 11 '15 at 14:35
  • thanks! i am using a library called geopy and it has vincenty... – user308827 Apr 11 '15 at 14:44
  • Ah! As mentioned in my comment to [this answer](http://stackoverflow.com/a/28368926/4014959) geopy provides both great-circle & Vincenty distance, with Vincenty being the default. – PM 2Ring Apr 12 '15 at 06:51

2 Answers2

3

Yes, first select the minimum of B, then add it to every element of A.

b = min(B)
print([a + b for a in A])
orlp
  • 112,504
  • 36
  • 218
  • 315
  • as per my comment above, this is a toy example, in the real example A and B are lat and lon values and I compute the great circle distance between them. updated question – – user308827 Apr 11 '15 at 14:11
  • what if `A` has values in range [0, 1] – ZdaR Apr 11 '15 at 14:11
1

itertools.product isn't really appropriate for your Great Circle task because it lumps all the (A, B) pairs into one container, but you want to keep all the pairs with a given A value together in a sub-container so you can find their minimum. So just use a pair of nested loops. If you want, you can do it as a generator expression to calculate the minimum value nested inside a list comprehension, like this:

A = [50, 60, 70]
B = [0.3, 0.1, 0.5, 0.4]

f = lambda x, y: x + y

print [min(f(a,b) for b in B) for a in A]
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • thanks! is itertools.product inappropriate because it is difficult to specify a lambda? – user308827 Apr 11 '15 at 14:21
  • @user308827: It's inappropriate because it lumps all the (A, B) pairs into one container, but you want to keep all the pairs with a given A value together in a sub-container so you can find their minimum. – PM 2Ring Apr 11 '15 at 14:31