通过F#中的通用ID将多个数据列表合并在一起 [英] Merge multiple lists of data together by common ID in F#

查看:80
本文介绍了通过F#中的通用ID将多个数据列表合并在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个来自4个不同来源的数据列表,它们具有一组我想合并在一起的通用ID(基于ID),基本上以一个新列表结尾,每个ID一个,每个来源一个条目

I have multiple lists of data from 4 different sources with a common set of IDs that I would like to merge together, based on ID, basically ending up with a new list, one for each ID and a single entry for each source.

4个来源中的每个来源的输出列表中的对象看起来都像这样:

The objects in the output list from each of the 4 sources look something like this:

type data = {ID : int; value : decimal;}

例如,我会有:

let sourceA = [data1, data2, data3];
let sourceB = [data1, data2, data3];
let sourceC = [data1, data2, data3];
let sourceD = [data1, data2, data3];

(我意识到这段代码无效,只是试图给出一个基本的想法……列表实际上是从数据库中提取并生成的)

(I realize this code is not valid, just trying to give a basic idea... the lists are actually pulled and generated from a database)

然后我想获取sourceA,sourceB,sourceC和sourceD,并将它们处理到包含如下对象的列表中:

I would then like to take sourceA, sourceB, sourceC and sourceD and process them into a list containing objects something like this:

type dataByID = {ID : int; valueA : decimal; valueB : decimal; valueC : decimal; valueD : decimal; }

...以便我可以将它们以CSV格式打印出来,第一列是ID,而列2-5是来自源A-D的数据,对应于该行中的ID.

...so that I can then print them out in a CSV, with the first column being the ID and coulmns 2 - 5 being data from sources A - D corresponding to the ID in that row.

我是F#的新手,那么处理该数据的最佳方法是什么,以便我按ID匹配所有源数据值?

I'm totally new to F#, so what would be the best way to process this data so that I match up all the source data values by ID??

推荐答案

似乎您可以简单地连接所有列表,然后使用Seq.groupBy来获取一个包含输入列表中唯一ID以及与之关联的所有值的列表. ID.可以使用类似的方法完成此操作:

It seems that you could simply concatenate all the lists and then use Seq.groupBy to get a list that contains unique IDs in the input lists and all values associated with the ID. This can be done using something like:

let data = 
  [ data1; data2; data3; data4 ]   // Create list of lists of items 
  |> Seq.concat                    // Concatenate to get a single list of items
  |> Seq.groupBy (fun d -> d.ID)   // Group elements by ID

seq { for id, values in data -> 
        // ID is the id and values is a sequence with all values 
        // (that come from any data source) }

如果要关联源(是否为data1,请执行data2, etc...) with the value then you can first use map`操作以添加数据源的索引:

If you want to associate the source (whether it was data1, data2, etc...) with the value then you can first usemap` operation to add an index of the data source:

let addIndex i data = 
  data |> Seq.map (fun v -> i, v)

let data = 
  [ List.map (addIndex 1) data1;
    List.map (addIndex 2) data2;
    List.map (addIndex 3) data3;
    List.map (addIndex 4) data4 ]
  |> Seq.concat
  |> Seq.groupBy (fun (index, d) -> d.ID)

现在,data还包含数据源的索引(从1到3),因此在遍历值时,可以使用索引来找出该项目来自哪个数据源.甚至可以使用Seq.mapi编写更好的版本,以遍历数据源列表并自动将索引添加到所有值:

Now, data also contains index of the data source (from 1 to 3), so when iterating over the values, you can use index to find out from which data source the item comes from. Even nicer version can be written using Seq.mapi to iterate over list of data sources and add index to all the values automatically:

let data = 
  [ data1; data2; data3; data4 ]
  |> Seq.mapi (fun index data -> Seq.map (addIndex index) data)
  |> Seq.concat
  |> Seq.groupBy (fun (index, d) -> d.ID)

这篇关于通过F#中的通用ID将多个数据列表合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆