我希望过滤掉字符串数组(String [] list)
列表中的字符串我试过
|> List.map (fun x-> Array.filter (fun x -> x = condition))
但它起作用了
有人可以帮忙吗?
感谢
编辑(更清楚)输入
[[|hi,hello,hello|],[|hello,hi,hello|],[|hi,hi,hello|]]
输出(过滤出来)
[[|hello,hello|],[|hello,hello|],[|hello|]]
答案 0 :(得分:3)
易:
yourList
|> List.map (Array.filter ((<>) condition))
为您的例子:
[[|"hi";"hello";"hello"|];[|"hello";"hi";"hello"|];[|"hi";"hi";"hello"|]]
|> List.map (Array.filter ((<>) "hi"))
在fsharpi中给出:
> [[|"hi";"hello";"hello"|];[|"hello";"hi";"hello"|];[|"hi";"hi";"hello"|]] |> List.map (Array.filter ((<>) "hi"));;
val it : string [] list =
[[|"hello"; "hello"|]; [|"hello"; "hello"|]; [|"hello"|]]
请注意,您必须使用"
和;
代替,
(或者您获得未定义符号的元组)
如果你想要一个函数来标记应该像这样的所有元素:
let shouldGo (s : string) : bool =
s <> "hi"
(注意每个函数string -> bool
都会这样做 - 它将删除所有数组元素a
shouldGo a = true
)
你必须像这样修改它:
myList
|> List.map (Array.filter (shouldGo >> not)) // yeah it reads strange - that's filters fault
或者您可以拥有通用功能:
let filterOut (shouldGo : 'a -> bool) =
List.map (Array.filter (shouldGo >> not))
并像这样使用它:
myList |> filterOut ((=) "hi")
不知道我还能添加什么;)
如果您需要过滤转换,可以使用此
let mapAndFilterOut (f : 'a -> 'b) (shouldGo : 'b -> bool) =
List.map (Array.filter (f >> shouldGo >> not))
你可以这样称呼你的照片(假设x是你的数组值):
likes |> mapAndFilter (fun x -> string (dataohumano TodosLosHumanos 0 x 3)) ((<>) genero)
但我认为你应该开始并为自己做一些实验;) (因为这确实是从错误的假设开始,就像你想要过滤掉,但在这里似乎你只想过滤,这些例子有点奇怪)
PS:在特殊情况中执行此操作:
let likesFiltradosGenero genero likes =
let mapF x = string (datoHumano Todos LosHumanos 0 x 3)
let filterF =
match genero with
| "h" -> mapF >> ((=) "h")
| "m" -> mapF >> ((<>) "m")
| g -> failwith ("unexpected genero: " + g)
likes
|> List.map (Array.filter filterF)
PPS:我会重命名那些mapF
和filterF
以显示他们的缩进但我不懂西班牙语(?),即使我能猜到&#34; genero&#34;是&#34;性别&#34;?
答案 1 :(得分:0)
据我所知,你有一个字符串数组列表,如:
let source = [ [| "abc"; "def" |]; [|"ghi"; "abc" |] ]
如果您不需要跟踪过滤后的字符串所属的数组,我会这样做:
source |> List.collect (List.ofArray) |> List.filter (fun x -> x = "abc")