我有三个数组,当前两个对齐时 - [|(“C”,“No”),(“C”,“是”); ... |] - 并由第三个过滤产生“seqs seqs” - [...; seq [(“C”,(“No”,1),(“Yes”,1)]; ...];:
<head>
<script type="text/javascript" src="js/vendor/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.28/angular-route.min.js"></script>
</head>
<body ng-app="StarWarsApp" \>
<div class="container">
<div ng-view></div>
</div>
<!-- Modules -->
<script type="text/javascript" src="js/app.js"></script>
<!-- Controllers -->
<script type="text/javascript" src="js/controllers/HomeController.js"></script>
<!-- Services -->
<script type="text/javascript" src="js/services/charactersList.js"></script>
<script type="text/javascript" src="node_modules/ne-swapi/dist/ne-swapi.min.js"></script>
</body>
</html>
如何生成预期结果?
答案 0 :(得分:3)
那么,基于所包含代码中的注释,您期望看到Seq&lt; SEQ&LT; ...&GT;&GT;数据,......是关键点。不幸的是,你不能这样做,因为如果你检查你的内部seq的元素,你使用了这个:(&#34; A&#34;,(&#34; No&#34; ,1),(&#34;是&#34;,3)。首先,有一个缺少的右括号(不是很大),第二,内部seq有混合类型。第一个元素是一个字符串,第二个元素是一个字符串,第二个第三个是string和int的元组。
通过对预期结果进行一些修改,您可以通过以下方式实现类似的操作:
let first = [| "C"; "D"; "C"; "E"; "B"; "A"; "A"; "E"; "A"; "B"; "A"; "D"; "B"; "E" |]
let second = [| "No"; "No"; "Yes"; "Yes"; "Yes"; "No"; "Yes"; "No"; "Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "No" |]
let fltr = [| "No"; "Yes" |]
let filterProduct (first:string[]) (second:string[]) (fltrs:string []) =
Array.zip first second
|> Array.groupBy fst
|> Array.map (fun (k, values) ->
k,
fltrs
|> Array.map (fun fltr ->
let c = values |> Array.map snd |> Array.filter (fun x -> x = fltr) |> Array.length
fltr, c
)
)
|> Array.sortBy (fun (k, v) -> k)
let third =
filterProduct first second fltr
printfn "third: %A" third
这样,third
具有以下格式:
seq<string * seq<string * int>>
答案 1 :(得分:0)
这与我想要的非常接近!
module SOANS =
open System
let first = [| "C"; "D"; "C"; "E"; "B"; "A"; "A"; "E"; "A"; "B"; "A"; "D"; "B"; "E" |]
let firstFilter = [| "A"; "B"; "C"; "D"; "E" |]
let second = [| "No"; "No"; "Yes"; "Yes"; "Yes"; "No"; "Yes"; "No"; "Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "No" |]
let secondFilter = [| "No"; "Yes" |]
let filterProduct (first:string[]) (second:string[]) (secondFilter:string) =
Array.zip first second
|> Array.filter (fun (_, s) ->
Seq.forall (fun c -> s.Contains (string c)) secondFilter)
|> Array.map fst
|> Array.toSeq
let third =
[for i in [0..firstFilter.Length - 1] do
yield (filterProduct second first firstFilter.[i])]
|> List.countBy id
|> List.toSeq
let fourth =
third
|> Seq.map (fun (s, _) -> s |> (Seq.countBy id) |> Seq.sort)
printfn ""
printfn "fourth:"
fourth |> Seq.iter (fun (c) -> (printf "%A; " c))
// Expected Result: fourth: [seq [("A", ("No", 1), ("Yes", 3)];
// seq [("B", ("No", 0), ("Yes", 3)];
// seq [("C", ("No", 1), ("Yes", 1)];
// seq [("C", ("No", 1), ("Yes", 1)];
// seq [("C", ("No", 2), ("Yes", 1)]]
// Actual Result: fourth: seq [("No", 1); ("Yes", 3)];
// seq [("No", 0); ("Yes", 3)];
// seq [("No", 1); ("Yes", 1)];
// seq [("No", 1); ("Yes", 1)];
// seq [("No", 2); ("Yes", 1)];
答案 2 :(得分:0)
我不确定你是否想看零是或否。如果没有,以下代码可能有所帮助。
namespace FSharpBasics
module Classifier =
let private first =
[| "C"; "D"; "C"; "E"; "B"; "A"; "A"; "E";
"A"; "B"; "A"; "D"; "B"; "E" |]
let private second =
[| "No"; "No"; "Yes"; "Yes"; "Yes"; "No"; "Yes"; "No";
"Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "No" |]
//let private fltr =
// [| "No"; "Yes" |]
(* operations *)
let pair =
Array.zip
let classifier =
let intermediate =
pair first second
let result =
intermediate
|> Seq.groupBy (fun (first, _) -> first)
|> Seq.map (fun (first, grouping) ->
(first, grouping
|> Seq.groupBy (fun (_, second) -> second)
|> Seq.map (fun (second, grouping) ->
(second, grouping
|> Seq.length))))
seq {
for x in result do yield x
}
[<EntryPoint>]
let main argv =
classifier |> Seq.iter (printfn "%A")
System.Console.ReadKey() |> ignore
0
(* ---Result----
("C", seq [("No", 1); ("Yes", 1)])
("D", seq [("No", 1); ("Yes", 1)])
("E", seq [("Yes", 1); ("No", 2)])
("B", seq [("Yes", 3)])
("A", seq [("No", 1); ("Yes", 3)])
---- ----*)