gpt4 book ai didi

F# deedle 如何将帧转换为元组列表

转载 作者:行者123 更新时间:2023-12-02 20:04:17 25 4
gpt4 key购买 nike

假设我有一个如下的框架

type Person = 
{ Name:string; Age:int; Comp1:float; Comp2:float }

let peopleRecds =
[ { Name = "Joe"; Age = 51; Comp1=12.1; Comp2 =20.3 }
{ Name = "Tomas"; Age = 28; Comp1=1.1; Comp2 =29.3 }
{ Name = "Eve"; Age = 2; Comp1=2.1; Comp2 =40.3 }
{ Name = "Suzanne"; Age = 15; Comp1=12.4; Comp2 =26.3} ]
let peopleList = Frame.ofRecords peopleRecds

如何将 peopleList 转换为 元组列表

最佳答案

要创建元组可以使用 FSharpValue.MakeTuple

然后您需要指定类型或元组:

open Microsoft.FSharp.Reflection

type Person = { Name:string; Age:int; Comp1:float; Comp2:float }

let peopleRecds =
[ { Name = "Joe"; Age = 51; Comp1=12.1; Comp2 =20.3 }
{ Name = "Tomas"; Age = 28; Comp1=1.1; Comp2 =29.3 }
{ Name = "Eve"; Age = 2; Comp1=2.1; Comp2 =40.3 }
{ Name = "Suzanne"; Age = 15; Comp1=12.4; Comp2 =26.3} ]

let peopleList = Frame.ofRecords peopleRecds

let toListOfTuple (frame:Frame<_,_>) tupleType =
frame.Rows
|> Series.mapValues(Series.values >> Seq.toArray)
|> Series.mapValues(fun x -> FSharpValue.MakeTuple(x, tupleType))
|> Series.values
|> Seq.toList

let tupleType = typeof<(string*int*float*float)>

(peopleList, tupleType)
||> toListOfTuple
|> List.iter(printfn "%A")

打印:

("Joe", 51, 12.1, 20.3)
("Tomas", 28, 1.1, 29.3)
("Eve", 2, 2.1, 40.3)
("Suzanne", 15, 12.4, 26.3)

关于F# deedle 如何将帧转换为元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com