我知道您需要所有\u actors的元素之间的所有距离,以及所有init\u actors的元素之间的所有距离
我认为你们应该做笛卡尔积,然后绘制地图,得到所有的距离。
all_actors =["brad", "tom", "abc", "def"]
init_actors=["tom", "abc"]
# Create cartesian product of tables
d1=bj.filter(lambda x: x['actor'] in all_actors)
d2=centroids.filter(lambda x: x['actor'] in init_actors)
combinations = d1.cartesian(d2)
然后,您只需应用计算距离的贴图函数(我不确定笛卡尔的布局结果是什么,所以您必须弄清楚calculate\u cartesian应该是什么样子)。
combinations.map(calculate_euclidean)
编辑:我在google上搜索到cartesian生成成对的行(x,y)-x和y与所有/init\u actors的元素类型相同-所以您可以创建函数:
def calculate_euclidean(x, y):
dc={key: (x[key] - y[key])**2 for key in x.keys() if key not in 'actor'}
val=sum([v for v in dc.values()])
val=math.sqrt(val)
#returning dict, but you can change result row layout if you want
return {'value': val,
'actor1': x['actor']
'actor2': y['actor']}
所有距离计算操作都是分布式的,因此它应该运行得更快。