我有三个数组 - first 是 float 组,second 是字符串数组,fltr 是字符串数组。我需要根据第二个数组中的匹配索引是否包含过滤器数组元素中的所有字符来生成第一个数组中元素的乘积:
module SOQN =
open System
let first = [| 2.00; 3.00; 5.00; 7.00; 11.00 |]
let second = [| "ABCD"; "ABCE"; "ABDE"; "ACDE"; "BCDE" |]
let fltr = [| "AC"; "BD"; "CE" |]
let result =
first
|> Array.filter second // filter for elements containing characters in second array
|> Seq.reduce (fun x y -> x * y)
// Expected Result: let result = [| 42.00; 110.00; 231.00 |]
如何生成产品数组?
最佳答案
类似这样的事情
let first = [| 2.00; 3.00; 5.00; 7.00; 11.00 |]
let second = [| "ABCD"; "ABCE"; "ABDE"; "ACDE"; "BCDE" |]
let fltr = "AC"
Array.zip first second
|> Array.filter (fun (_, s) ->
Seq.forall (fun c -> s.Contains (string c)) fltr)
|> Array.map fst
|> Array.reduce (*)
关于arrays - F# 两个数组 - 第一个数组乘积按第二个数组上的索引过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47810083/