我想找出这对曾经接触过的人。以下是数据:
Input is
K-\> M, H
M-\> K, E
H-\> F
B-\> T, H
E-\> K, H
F-\> K, H, E
A-\> Z
字符串
输出为:
Output:
K, M //(this means K has supplied goods to M and M has also supplied some good to K)
H, F
型
下面是我写的代码。
from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession, SQLContext
from pyspark.ml.regression import LinearRegression
import re
from itertools import combinations
spark = SparkContext("local", "DoubleRDD")
def findpairs(ls):
lst = []
for i in range(0,len(ls)-1):
for j in range(i+1, len(ls)):
if ls[i] == tuple(reversed(ls[j])):
lst.append(ls[i])
return(lst)
text = spark.textFile("path to the .txt")
text = text.map(lambda s: s.replace("->",","))
text = text.map(lambda s: s.replace(",",""))
text = text.map(lambda s: s.replace(" ",""))
pairs = text.flatMap(lambda x: [(x[0],y) for y in x[1:]])
commonpairs = pairs.filter(lambda x: findpairs(x))
pairs.collect()
The output is: []
的字符串
2条答案
按热度按时间5w9g7ksd1#
不要使用RDD,这个问题可以使用本机spark框架函数来解决。
字符串
拆分并分解收件人(y)列
型
自连接 Dataframe ,其中左边的接收者是右边 Dataframe 中的发送者。然后过滤 Dataframe ,使左边的发送者和右边的接收者相同
型
删除发件人和收件人的重复组合
型
woobm2wo2#
字符串
我写了上面的代码,它产生了预期的输出。
型