F#适用于(字符串,浮点数,浮点数)三元组的容器?

时间:2022-11-01 01:37:56

I have the following problem and I hope somebody can help me.

我有以下问题,希望有人能帮助我。

Short description of the problem: i need to store a (string A, float B, float C) triad into a suitable container. The triad originates fomr a double "for" loop. But the essential point is that I will need to slice this container when the loops are over to perform other operations.

问题的简短描述:我需要将(字符串A,浮点B,浮点C)三元组存储到合适的容器中。三合一起源于双重“for”循环。但关键是我需要在循环结束时切片此容器以执行其他操作。

An example that can be executed from the .fsx shell (using Deedle frames) follows. The triad is what is beeing printed on the screen.

可以从.fsx shell(使用Deedle框架)执行的示例如下。三合会是印在屏幕上的东西。

open Deedle

let categorical_variable = [| "A"; "B"; "C"; "A"; "B"; "C"; |]
let vec_1 = [| 15.5; 14.3; 15.5; 14.3; 15.5; 14.3; |]
let vec_2 = [| 114.3; 17.5; 9.3; 88.7; 115.5; 12.3; |]

let dframe = frame ["cat" =?> Series.ofValues categorical_variable
                    "v1" =?> Series.ofValues vec_1
                    "v2" =?> Series.ofValues vec_2 ]

let distinct_categorical_variables = categorical_variable |> Array.toSeq |> Seq.distinct |> Seq.toArray

let mutable frame_slice : Frame<int, string> = Frame.ofRows []
let mutable frame_slice_vec_1 : float[] = Array.empty
let mutable frame_slice_vec_1_distinct : float[] = Array.empty

for cat_var in distinct_categorical_variables do

    frame_slice <- (dframe |> Frame.filterRowValues (fun row -> row.GetAs "cat" = cat_var))
    frame_slice_vec_1 <- (frame_slice?v1).Values |> Seq.toArray
    frame_slice_vec_1_distinct <- (frame_slice_vec_1 |> Array.toSeq |> Seq.distinct |> Seq.toArray)

    for vec_1_iter in frame_slice_vec_1_distinct do

        printfn "%s, %f, %f \n"  cat_var vec_1_iter (Array.average ((frame_slice?v2).Values |> Seq.toArray) ) |> ignore

So, is there any suitable object where to store this triad? I saw Array3d objects, but I don't think they are the right solution cause A, B and C of my triad have different types.

那么,有没有合适的对象存放这个三合一?我看到了Array3d对象,但我不认为它们是正确的解决方案因为我的三元组的A,B和C有不同的类型。

Many thanks in advance.

提前谢谢了。

1 个解决方案

#1


you probably want a sequence expression with tuples:

你可能想要一个带有元组的序列表达式:

let mySequence = 
  seq { for cat_var in distinct_categorical_variables do
          ...
          for vec_1_iter in ... do
            yield cat_var, vec_1_iter, Array.average ... }

// then use it like
for cat_var, vec_1_iter, result in mySequence do
  ...

#1


you probably want a sequence expression with tuples:

你可能想要一个带有元组的序列表达式:

let mySequence = 
  seq { for cat_var in distinct_categorical_variables do
          ...
          for vec_1_iter in ... do
            yield cat_var, vec_1_iter, Array.average ... }

// then use it like
for cat_var, vec_1_iter, result in mySequence do
  ...