首页 > 解决方案 > 如何使用地图功能?

问题描述

所以我已经定义了许多类型和将数据映射到这些记录/类型的函数。现在我需要将每个记录/类型的内容映射到一个“主”记录中,该记录将包含以前记录中的所有内容,基于一个键,在这种情况下是状态。我不知道该怎么做,但我在下面进行了尝试,以及更多提供上下文的代码。有什么建议么?如果您需要更多信息,请发表评论。

我有这样定义的类型:

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

标签: f#

解决方案


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

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"; Education = 1; Income = 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;};}); (“纽约”,{州 =“纽约”;StateEdu = {州 =“纽约”;教育 = 1;收入 = 79215.0;};StateFamily = {州 =“纽约”;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;

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


推荐阅读