Scala Spark:如何比较两个元组清单

发布于 2025-02-09 03:26:46 字数 628 浏览 2 评论 0原文

我想发现我的第一个阵列中的每个元组是否存在于我的第二个元组中。 如果不是这种情况,我想返回所有与不匹配的元组以及元素的哪个元素完全不匹配。

可能是: 对于firstArray中的每个元素(x,y): 对于secondarray中的每个元素(k,z): 如果(x!= k)打印(某物) 返回(x,y) 如果(y!= z)打印(某物) 返回(x,y) 示例:

val firstArray: Array[(String,String)] = Array(("elem1","elem2"), ("elem3","elem4"))
val secondArray: Array[(String,String)] = Array(("elem1","elem2"), ("elem5","elem4"), ("elem3","elem7"))

所需的输出 输出:

("elem3","elem4") is eliminated because elem4 doesn't match elem7
    val result: Array[(String,String)] = Array(("elem3","elem4"))

I would like to find if each Tuple in my firstArray exists in my secondArray of tuple.
If it is not the case, i would like to return all tuple that doesn't match and which element of tuple exactly doesn't match.

It could be something like :
for each element (x,y) in firstArray:
for each element (k,z) in secondArray:
if (x != k) print(something)
return (x,y)
if (y != z) print(something)
return (x,y)
Example:

val firstArray: Array[(String,String)] = Array(("elem1","elem2"), ("elem3","elem4"))
val secondArray: Array[(String,String)] = Array(("elem1","elem2"), ("elem5","elem4"), ("elem3","elem7"))

Desired output
Output:

("elem3","elem4") is eliminated because elem4 doesn't match elem7
    val result: Array[(String,String)] = Array(("elem3","elem4"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2025-02-16 03:26:46

您可以尝试类似的内容

val res = firstArray.filterNot(secondArray.contains(_))

将返回第二个数组中不存在的第一个数组的元素。

编辑

以下代码将在两个阵列上循环,并比较元组

for {
  (i,j) <- firstArray
  (k,l) <- secondArray
}
{
  println((i,j) match {
    case (a,b) if (a == k && b ==l) => "Tuple found"
    case (a,_) if (a == k)=> "First elem only found."
    case (_,b) if (b ==l)=> "Second elem only found."
    case _ => "No match"
  })
}

希望这会有所帮助

you can try something like

val res = firstArray.filterNot(secondArray.contains(_))

It will return the elements of first array that are not present in the second.

Edit

The following code will loop over the two arrays, and compare the tuples

for {
  (i,j) <- firstArray
  (k,l) <- secondArray
}
{
  println((i,j) match {
    case (a,b) if (a == k && b ==l) => "Tuple found"
    case (a,_) if (a == k)=> "First elem only found."
    case (_,b) if (b ==l)=> "Second elem only found."
    case _ => "No match"
  })
}

Hope this will help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文