将float [,]转换为f#中的列表? [英] Converting float[,] to list in f#?
问题描述
我有一个可以很好地用于列表的功能,但是该功能的输入来自外部系统/语言,是float[,]
.
I have a function that works fine for lists however the input to the function comes as float[,]
from external system / language.
我阅读了此,但是当我应用此时,我得到了一个错误float[,] is not compatible with Seq<a'>
.但是,此列表也只包含浮点数.
I read this but when I apply this I get an error float[,] is not compatible with Seq<a'>
. However this list is also only of floats.
列表功能:
let aggrArraysL data =
data
|> Seq.groupBy (fun (a, b, c) -> a)
|> Seq.map (fun (key, group) ->
group |> Seq.reduce (fun (a, b, c) (x, y, z) -> a, b + y, (b * c + y * z * 1.)/(b + y)))
数组尝试:
let aggrArrays (data2 : float[,]) =
data2
|> Seq.toList
|> Seq.groupBy (fun (a, b, c) -> a)
|> Seq.map (fun (key, group) ->
group |> Seq.reduce (fun (a, b, c) (x, y, z) -> a, b + y, (b * c + y * z * 1.)/(b + y)))
|> Seq.toArray
我要去哪里错了?谢谢!
Where am I going wrong? thanks!
推荐答案
2D数组实现了IEnumerable
,因此您可以使用Seq.cast<T>
进行投射:
2D arrays implement IEnumerable
so you can just cast them using Seq.cast<T>
:
let toList (data2: float[,]) = data2 |> Seq.cast<float> |> Seq.toList
您可能想使其通用:
let toList<'a> (data2: 'a [,]) = data2 |> Seq.cast<'a> |> Seq.toList
看起来您想将数组映射到行元素列表中,而不是将其展平.在这种情况下,您可以执行以下操作:
It looks like you want to map the array into a list of row elements instead of flattening it. In this case you could do:
let aggrArrays (data2:float[,]) =
[0 .. (Array2D.length1 data2) - 1]
|> List.map (fun i -> (data2.[i, 0], data2.[i, 1], data2.[i, 2]))
|> Seq.groupBy id
|> Seq.map (fun (key, group) -> group |> Seq.reduce (fun (a,b,c) (x, y, z) -> a, b+y , (b*c+y*z*1.)/(b+y)))
|> Seq.toArray
这篇关于将float [,]转换为f#中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!