Reputation: 1267
I have a list of tuples that are coordinates: xy = [(x_1, y_1), (x_2, y_2), ...]
and an specific point xy_sp = [x_sp, y_sp]
. I want to calculate the distance from each point to the so called "specific point" and to create a dictionary in the form: {coordinates_of_1st_point: distance_1, coordinates_of_2nd_point: distance_2, ...}
and then perhaps I just can order my dictionary by values.
I want to do this with a functional pythonic approach. So I did:
import numpy as np
distances = list(map(lambda k: np.linalg.norm(np.asarray(k) - np.asarray(xy)), xy))
And then I just created a dictionary comprehension:
dict_distances = {k : v for k, v in zip(xy, distances)}
It is this last point that I want to do with a maps and lambdas in a functional approach but I don't know how to do it. If it can be all done in a line of code it would be interesting, although I suppose it will be not readable at all and therefore not very recommendable.
Any suggestions are welcomed.
Upvotes: 1
Views: 355
Reputation: 78554
You don't need a lambda
for that, just call the built-in dict
on the result from zip
:
dict_distances = dict(zip(xy, distances))
Builds a mapping from the 2-tuple returned by zip
.
Upvotes: 3