如何使用地图功能?

时间:2018-06-14 22:31:01

标签: f#

所以我有很多已定义的类型和将数据映射到这些记录/类型的函数。现在我需要将每个记录/类型的内容映射到“主”记录中,该记录将包含基于密钥的先前记录中的所有内容,在本例中为密钥。我不知道该怎么做,但是我在下面尝试了,还有更多的代码来提供上下文。有什么建议?如果您需要更多信息,请发表评论。

我的类型定义如下:

type StateEdu = 
    { State : string
      Education : int
      Income : float }

type StateFamily =
    { State : string
      PctMoreThan4Children : float
      PctFamilyMorethan3 : float }

但是现在我需要做一些像这样的事情(当我跑的时候这不起作用):

let stateall = statemap.Keys

let statedatamap =
 stateall
 |> Seq.map (fun state ->
    state,
    {State = state
     StateEdu = StateEdu.[state]
     StateFamily = StateFamily.[state]
     })
|> Map.ofSeq

1 个答案:

答案 0 :(得分:0)

由于您缺少变量和类型,因此不太清楚您的所需输出是什么。假设您想要一个包含州的密钥的新地图/字典,然后是一个既具有教育状态又包含家庭状态的记录,一种方法是: (我添加了类型并修复了一些错误)

type StateEdu = 
    { State : string
      Education : int
      Income : float }

type StateFamily =
    { State : string
      PctMoreThan4Children : float
      PctFamilyMorethan3 : float }

//I added this new type so this is value of the final map.    
type State2 = 
    { State : string
      StateEdu : StateEdu
      StateFamily : StateFamily }

//Instead providing the csv it's better to minimize the example, so here are the two record lists.      
let stateEdu = 
  [{State = "TX"; Income = 51522.; Education = 0}
   {State = "AL"; Income = 6481.; Education = 1}
   {State = "MO"; Income = 78921.; Education = 1}
   {State = "TN"; Income = 12000.; Education = 4}
   {State = "PA"; Income = 79850.; Education = 2}
   {State = "NY"; Income = 79215.; Education = 1}
   {State = "CA"; Income = 79045.; Education = 2}]

let datafamily = 
  [{State = "TX"; PctMoreThan4Children = 51.52; PctFamilyMorethan3 = 65.0}
   {State = "AL"; PctMoreThan4Children = 64.00; PctFamilyMorethan3 = 51.4}
   {State = "MO"; PctMoreThan4Children = 78.92; PctFamilyMorethan3 = 25.1}
   {State = "TN"; PctMoreThan4Children = 12.00; PctFamilyMorethan3 = 62.1}
   {State = "PA"; PctMoreThan4Children = 8.50; PctFamilyMorethan3 = 41.2}
   {State = "NY"; PctMoreThan4Children = 25.15; PctFamilyMorethan3 = 31.0}
   {State = "CA"; PctMoreThan4Children = 79.5; PctFamilyMorethan3 = 50.5}]


let stateedu =
     stateEdu
     |> Seq.map (fun x -> x.State,x)
     |> Map.ofSeq

let datafam =
     datafamily
     |> Seq.map (fun x -> x.State,x)
     |> Map.ofSeq

//This is one way to quickly extract the keys
let stateall = stateedu |> Map.toSeq |> Seq.map fst  

//We go through all the keys
let statedatamap =
 stateall
 |> Seq.map (fun state ->
    state,
    {State = state //this is the State2type
     StateEdu = stateedu.[state] 
     StateFamily = datafam.[state]
     })
|> Map.ofSeq

输出:

  

val statedatamap:Map = map       [(“AL”,{State =“AL”;                StateEdu = {State =“AL”;                            教育= 1;                            收入= 6481.0;};                StateFamily = {State =“AL”;                               PctMoreThan4Children = 64.0;                               PctFamilyMorethan3 = 51.4;};});        (“CA”,{State =“CA”;                StateEdu = {State =“CA”;                            教育= 2;                            收入= 79045.0;};                StateFamily = {State =“CA”;                               PctMoreThan4Children = 79.5;                               PctFamilyMorethan3 = 50.5;};});        (“MO”,{State =“MO”;                StateEdu = {State =“MO”;                            教育= 1;                            收入= 78921.0;};                StateFamily = {State =“MO”;                               PctMoreThan4Children = 78.92;                               PctFamilyMorethan3 = 25.1;};});        (“NY”,{State =“NY”;                StateEdu = {State =“NY”;                            教育= 1;                            收入= 79215.0;};                StateFamily = {State =“NY”;                               PctMoreThan4Children = 25.15;                               PctFamilyMorethan3 = 31.0;};});        (“PA”,{State =“PA”;                StateEdu = {State =“PA”;                            教育= 2;                            收入= 79850.0;};                StateFamily = {State =“PA”;                               PctMoreThan4Children = 8.5;                               PctFamilyMorethan3 = 41.2;};});        (“TN”,{State =“TN”;                StateEdu = {State =“TN”;                            教育= 4;                            收入= 12000.0;};                StateFamily = {State =“TN”;                               PctMoreThan4Children = 12.0;                               PctFamilyMorethan3 = 62.1;};});        (“TX”,{State =“TX”;                StateEdu = {State =“TX”;                            教育= 0;                            收入= 51522.0;};                StateFamily = {State =“TX”;                               PctMoreThan4Children = 51.52;                               PctFamilyMorethan3 = 65.0;};})]

如有必要,您可以定义输出类型以将两个记录都包含在一个项目中。例如,您始终可以按statedu.[state].Education索引记录。