F# 比较元组数组并返回不同的元素和索引

发布于 2024-12-11 18:52:24 字数 544 浏览 0 评论 0原文

#light

let a1 = [| (1, 1); (2, 1); (3, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1) |]

let aa = Array.zip a1 a2
       |> Array.filter(fun (x, y) -> x <> y)

我想编写一个函数来执行此操作:它将返回两个数组中的不同元组,但我还想返回第二个数组中不同元组的索引以及第二个数组中相应元组的索引。 (我的代码还没有完全工作!) 对于上面的示例,我想返回: 1 和 (2, 3) 再比如:

let a1 = [| (1, 1); (2, 1); (3, 1); (4, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1); (4, 2) |]

我要返回:1 and (2, 3); 3 和 (4, 2) 如果您有任何想法,请告诉我您的代码。 另外,我不习惯F#的新地方,这种格式让我感觉很难找到一个好的地方来发布我的问题,因此,我仍然在这里发布我的问题。

#light

let a1 = [| (1, 1); (2, 1); (3, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1) |]

let aa = Array.zip a1 a2
       |> Array.filter(fun (x, y) -> x <> y)

I want to write a function to do this: it will return the different tuple from two arrays, but I also want to return the index of the different tuple in the second array and the corresponding tuple in the second array. (My code did not work totally yet!)
For my above example, I want to return: 1 and (2, 3)
Another example:

let a1 = [| (1, 1); (2, 1); (3, 1); (4, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1); (4, 2) |]

I want to return: 1 and (2, 3); 3 and (4, 2)
If you have any idea, please show me your code.
Besides, I am not used to the new place for F#, the format makes me feel difficult to find a good place to post my questions, therefore, I still post my question here.

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

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

发布评论

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

评论(3

潦草背影 2024-12-18 18:52:24
let a1 = [| (1, 1); (2, 1); (3, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1) |]

let diff = 
    (a1, a2) 
    ||> Array.mapi2(fun i t1 t2 -> (i, t1, t2))
    |> Array.choose(fun (i, a, b) -> if a <> b then Some (i, b) else None)
let a1 = [| (1, 1); (2, 1); (3, 1) |]

let a2 = [| (1, 1); (2, 3); (3, 1) |]

let diff = 
    (a1, a2) 
    ||> Array.mapi2(fun i t1 t2 -> (i, t1, t2))
    |> Array.choose(fun (i, a, b) -> if a <> b then Some (i, b) else None)
心凉 2024-12-18 18:52:24

这是一种方法:

let diff a b =
  let s = Set.ofSeq a
  b 
  |> Seq.mapi (fun i x -> i, x)
  |> Seq.filter (fun (_, x) -> not (Set.contains x s))

示例

let a1 = [| (1, 1); (2, 1); (3, 1) |]
let a2 = [| (1, 1); (2, 3); (3, 1) |]
diff a1 a2 //output: seq [(1, (2, 3))]

这适用于任何集合(listarrayseq<_>set 等)并且序列可以具有不同的长度。如果您知道您将始终使用相同长度的数组,则可以进行相应的优化(请参阅 desco 的答案)。

Here's one way to do it:

let diff a b =
  let s = Set.ofSeq a
  b 
  |> Seq.mapi (fun i x -> i, x)
  |> Seq.filter (fun (_, x) -> not (Set.contains x s))

Example

let a1 = [| (1, 1); (2, 1); (3, 1) |]
let a2 = [| (1, 1); (2, 3); (3, 1) |]
diff a1 a2 //output: seq [(1, (2, 3))]

This works for any collection (list, array, seq<_>, set, etc) and the sequences may be of different lengths. If you know you'll always be using arrays of equal length, you can optimize accordingly (see desco's answer).

白日梦 2024-12-18 18:52:24
let diff (a:(int * int)[]) b =
  b
  |> Array.mapi (fun i tp -> if a.[i] <> tp then (i, tp) else (-1, tp))
  |> Array.filter (fun (x, _) -> x >= 0) 

演示

 > diff a1 a2;;
val it : (int * (int * int)) [] = [|1, (2, 3)); (3, (4, 2))|]
let diff (a:(int * int)[]) b =
  b
  |> Array.mapi (fun i tp -> if a.[i] <> tp then (i, tp) else (-1, tp))
  |> Array.filter (fun (x, _) -> x >= 0) 

DEMO

 > diff a1 a2;;
val it : (int * (int * int)) [] = [|1, (2, 3)); (3, (4, 2))|]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文