I am trying to convert a labda function in normal function but unable to understand it.
def closest(u):
return min(updated_unique_list, key=lambda v: len(set(u) ^ set(v)))
how this labda and min works?
I tried to understand from docs but I want to understand with this example and to create a normal function instead of this lambda function.
minfind the minimum value of an iterable with respect to a function.min(lst, key=func)in Python can be roughly considered as (ignoring details like callingfas little as possible or walking through the list only once)In your case, it finds the item
vinupdated_unique_listsuch thatlen(set(u) ^ set(v))is minimum.Now,
^between two set is their symmetric difference.set(u) ^ set(v)returns a set of elements that does not appear in bothuandv. The size of the result will be zero ifuandvare equal, and is maximum if they do not overlap at all.Therefore, the function tries to find an object in the
updated_unique_listwhich has the most common elements withu.If you want the “normal function” just expand the
minaccording to the algorithm I’ve described above.