@scrwtp提供了一个非常有用的函数(toJagged):
let toJagged<'a> (arr: 'a[,]) : 'a [][] =
[| for x in 0 .. Array2D.length1 arr - 1 do
yield [| for y in 0 .. Array2D.length2 arr - 1 -> arr.[x, y] |]
|]
Run Code Online (Sandbox Code Playgroud)
从2D数组转换为锯齿状数组.是否有可用的等效函数(toArray2D)用于从锯齿状数组转换为2D数组(假设锯齿状数组中的每一行具有相同数量的元素)?
我试图改编一个WikiBooks示例,以接受csv输入和类型,但是在将传入的类型列表转换为字典并匹配用户输入方面很费劲。
// https://en.wikibooks.org/wiki/F_Sharp_Programming/Sets_and_Maps#Examples_2
module SOQN =
open System
open FSharp.Data
type Country = Country of string
type City = City of string
type CountryCapital = {
Country:Country
City:City
}
let [<Literal>] sampleCsv = @"D:\Country_Capitals.csv"
type Capitals = CsvProvider<sampleCsv, Separators=",", HasHeaders=true>
let readFromCsvFile (fileName:string) =
let data = Capitals.Load(fileName)
[ for row in data.Rows do
yield { Country = Country row.Country; City = City row.City; } ]
let countryCapitals =
readFromCsvFile sampleCsv
// -> |> Map.ofList
Console.Write("Find capital by country (type …Run Code Online (Sandbox Code Playgroud) 我需要根据与prcntl中百分位数的比较将tmp中的值映射到tmpRplcd中的标签,但是Array.map2行会因为数组的长度不同而失败.
module SOQN =
open System
open MathNet.Numerics.Statistics
let tmp = [| 13.0; 17.0; 23.0; 11.0; 11.0; 13.0; 31.0;
19.0; 47.0; 29.0; 29.0; 19.0; 43.0; 37.0 |]
let tmpPrcntls =
tmp
|> Array.sort
let lbls = [| "p0"; "p1"; "p2"; "p3"; "p4";"p5" |]
let prcntls = [| Statistics.Percentile(tmpPrcntls,0) // 11.0
Statistics.Percentile(tmpPrcntls,20) // 13.0
Statistics.Percentile(tmpPrcntls,40) // 19.0
Statistics.Percentile(tmpPrcntls,60) // 28.6
Statistics.Percentile(tmpPrcntls,80) // 35.8
Statistics.Percentile(tmpPrcntls,100) // 47.0
|]
let lkpTbl = Map(Array.zip prcntls lbls) …Run Code Online (Sandbox Code Playgroud) 我有三个阵列- 第一是一个float数组,第二是一个字符串数组,和第三是一个浮子阵列,其包括从所述已排序的,唯一值第一阵列.
module SOQN =
open System
type collective = { score:double; yes:int; no:int; correct:double }
let first = [| 25; 20; 23; 10; 8; 5; 4; 12; 19; 15; 15; 12; 11; 11 |]
let second = [| "No"; "No"; "Yes"; "Yes"; "Yes"; "No"; "Yes"; "No"; "Yes"; "Yes"; "Yes"; "No"; "Yes"; "No" |]
let third = Array.distinct (first |> Array.sort)
let fourth = Seq.zip first second
let fifth = fourth |> Seq.sortBy …Run Code Online (Sandbox Code Playgroud)