如何从序列迭代中提取数据 [英] How to extract data from iterating a sequence

查看:150
本文介绍了如何从序列迭代中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

csvData1将数据包含在.csv文件中.我已经从电子表格中的仅两列中创建了一个序列 ("GIC-ID", "COVERAGE DESCRIPTION")

csvData1 contains the data in a .csv file. I've created a sequence out of just two of the columns in the spreadsheet ("GIC-ID", "COVERAGE DESCRIPTION")

let mappedSeq1 = 
   seq { for csvRow in csvData1 do yield (csvRow.[2], csvRow.[5]) }

在Visual Studio调试器中查找x最终会成为System.Tuple<string,string>.

Looking in the Visual Studio debugger x winds up being a System.Tuple<string,string>.

for x in mappedSeq1 do
    printfn "%A" x
    printfn "%A" x.ToString

这是执行的结果

for x in mappedSeq1

("GIC-ID", "COVERAGE DESCRIPTION")
<fun:main@28>

我很难弄清楚如何访问x,因此我可以提取第一个元素.

I am having difficulty figuring out how to access x, so I can extract the first element.

推荐答案

您可以使用模式匹配来解构元组

You can use pattern matching to deconstruct the tuple

for (a, b) in mappedSeq1 do
  // ...

for x in mappedSeq1 do
  let a, b = x

对于2元组,您也可以使用内置函数fstsnd

Alternatively for a 2-tuple you can use the built-in function fst and snd

这篇关于如何从序列迭代中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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