Spark find key/value pairs with key equals to other values and join
By : Drew
Date : March 29 2020, 07:55 AM
seems to work fine Using purely Scala collection functions (in Set) - I don't use Spark: code :
val ex = Set("T" -> "V", "V" -> "W", "A" -> "B", "B" -> "C")
val keysEquallingValues = ex.flatMap { tuple =>
ex.find(t => tuple._2 == t._1).map(t => tuple -> t)
}
val r = ex ++ keysEquallingValues.map(pair => pair._1._1 -> pair._2._2)
|
How to convert list pairs into tuple pairs
By : Marco Constâncio
Date : March 29 2020, 07:55 AM
hope this fix your issue How do you turn a list that contain pairs into a list that contains tuple pairs by using easy programming e.g for loop? x,y = ...? code :
def read_numbers():
numbers = ['68,125', '113,69', '65,86', '108,149', '152,53', '78,90']
return [tuple(map(int,pair.split(','))) for pair in numbers]
|
Comparing a tuple of a pair to a list of tuple pairs
By : lydia oye
Date : March 29 2020, 07:55 AM
it should still fix some issue Assuming the following function calculate the distance within 2 points: code :
def distance(point_a, point_b):
"""Returns the distance between two points."""
x0, y0 = point_a
x1, y1 = point_b
return math.fabs(x0 - x1) + math.fabs(y0 - y1)
def nearest(point, all_points):
closest_point, best_distance = None, float("inf")
for other_point in all_points:
d = distance(point, other_point)
if d < best_distance:
closest_point, best_distance = other_point, d
return closest_point
def nearest(point, all_points):
"""Returns the closest point in all_points from the first parameter."""
distance_from_point = functools.partial(distance, point)
return min(all_points, key=distance_from_point)
|
Pick from list of tuple combination pairs such that each tuple element appears at least twice
By : ScrambledEgg
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The easiest way to get each name exactly twice is the following, I guess: code :
lst = ["John", "Mike", "Mary", "Jane"] # not shadowing 'list'
pairs = list(zip(lst, lst[1:]+lst[:1]))
pairs
# [('John', 'Mike'), ('Mike', 'Mary'), ('Mary', 'Jane'), ('Jane', 'John')]
|
Filter a list of pairs (tuples) where the tuple doesn't include any value from another list
By : Nguyễn Thanh Hoàng
Date : March 29 2020, 07:55 AM
With these it helps I have a list of tuples: , Flat is better than nested code :
blacklist = {p[0] for p in blacklist_of_tuples}
[p for p in my_list if p[0] not in blacklist and p[1] not in blacklist]
[p for p in my_list if not any(el in blacklist for el in p)]
|