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)
我想编写一个函数来执行此操作:它将返回两个数组中的不同元组,但我还想返回第二个数组中不同元组的索引以及第二个数组中相应元组的索引。 (我的代码还没有完全工作!) 对于上面的示例,我想返回: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法:
示例
这适用于任何集合(
list
、array
、seq<_>
、set 等)并且序列可以具有不同的长度。如果您知道您将始终使用相同长度的数组,则可以进行相应的优化(请参阅 desco 的答案)。
Here's one way to do it:
Example
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).演示
DEMO