F#滤波器的二维阵列具有多个参数 [英] F# filter a two-dimensional array with multiple arguments

查看:105
本文介绍了F#滤波器的二维阵列具有多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经坚持了一段时间这个看似基本的问题。我有字符串的二维阵列和串的另一个一维阵列。的一维阵列包括一些在二维阵列的其中一列的元素present的。我希望得到的结果是二维阵列是由二维数组中的元素过滤。作为一个例子:

I've been stuck for a while with this seemingly basic problem. I have a two dimensional array of strings and another one dimensional array of strings. The one dimensional array consists of some of the elements present in one of the columns of the two dimensional array. The result that I wish to get is a two dimensional array which is filtered by the elements in the two-dimensional array. As an example:

二维数组:

[A,大象],[B,狗],[C,猫],[D,老鼠],[E,长颈鹿]]

[["A", "elephant"], ["B", "dog"] , ["C", "cat"] , ["D", "mouse"], ["E", "giraffe"]]

一维数组:

[大象,猫,长颈鹿]

["elephant" , "cat" , "giraffe"]

期望的结果:

[A,大象],[C,猫],[E,长颈鹿]]

[["A", "elephant] , ["C", "cat"] , ["E", "giraffe"]]

我感谢你在您的帮助。我是pretty新F#和努力学习一直难以直到现在。

I thank you in advance for your help. I'm pretty new to F# and trying to learn it has been difficult until now.

欢呼声

推荐答案

答案取决于你真正想做的事,但它听起来像是找到合适的再presentation是问题的最重要的部分。在你的榜样,您的嵌套列表中总是包含只有两个值(例如A和大象),所以它会更有意义使用元组的列表:

The answer depends on what you actually want to do, but it sounds like finding the right representation is the most important part of the question. In your example, your nested lists always contain just two values (e.g. "A" and "elephant") and so it would make more sense to use a list of tuples:

let things = [ ("A", "elephant"); ("B", "dog"); ("C", "cat");
               ("D", "mouse"); ("E", "giraffe")]

这再presentation会使事情变得更容易,因为我们只需要检查的元组的第二个元素是用于过滤列表:

This representation will make things easier, because we only need to check if the second element of the tuple is in the list used for filtering:

let filter = ["elephant" ; "cat" ; "giraffe"]

要做到这一点,你可以使用 List.filter 来筛选列表。在此条件下,您可以使用获得的动物 SND (获得一个元组的第二个元素),然后使用 List.exist ,看看它是否是在动物中的列表中要包括:

To do that, you can use List.filter to filter a list. In the condition, you can get the animal using snd (get second element of a tuple) and then use List.exist to see if it is in the list of animals to be included:

things |> List.filter (fun nested ->
  let animal = snd nested
  filter |> List.exists (fun a -> a = animal))

如果你想查找更有效,你可以创建一组过滤项目:

If you want to make the lookup more efficient, you can create a set of filtered items:

let filter = set ["elephant" ; "cat" ; "giraffe"]
things |> Seq.filter (fun nested -> filter.Contains(snd nested))

而且,事实上,你可以用函数组合来调用 SND 其次检查:

things |> Seq.filter (snd >> filter.Contains)

这意味着同样的事情与上面的线 - 它需要的文字和动物名的元组,使用 SND 函数提取动物的名称,然后传递命名为 filter.Contains ,看它是否是集合。

This means exactly the same thing as the line above - it takes the tuple with the letter and animal name, extracts the animal name using the snd function and then passes the name to filter.Contains to see if it is in the set.

这篇关于F#滤波器的二维阵列具有多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆