F#:通过将其中一个元素分组到数组中来减少元组列表

时间:2014-08-20 00:35:37

标签: arrays list f# tuples

我有一个元组列表,我希望将其元素之一作为键分组。例如,如果我有这个元组列表:

  

[(A," hello"),(A," stack"),(A," over"),(A," flow"),(B," how"),(B,   "你"),(C," doin")]

我想在表格中得到一个结果:

  

[(A,[" hello"," stack"," over"," flow"]),(B,[ "","你"]),(C,   ["杜安"])]

我是F#的新手,所以我完全不知道如何做到这一点。我提前谢谢你。

欢呼声

1 个答案:

答案 0 :(得分:3)

我认为您对列表元素使用了错误的分隔符 - 而不是,,您需要使用;

要获得结果,请使用以下代码段:

[("A", "hello"); ("A", "stack"); ("A","over"); ("A", "flow"); ("B", "how"); ("B", "you"); ("C", "doin")]
  |> Seq.groupBy fst 
  |> Seq.map (fun (key,groupping) -> key, (groupping |> Seq.map snd |> Seq.toList)) 
  |> Seq.toList