问题
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]) }
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
Here is the result of executing
for x in mappedSeq1
("GIC-ID", "COVERAGE DESCRIPTION")
<fun:main@28>
I am having difficulty figuring out how to access x
, so I can extract the first element.
回答1:
You can use pattern matching to deconstruct the tuple
for (a, b) in mappedSeq1 do
// ...
or
for x in mappedSeq1 do
let a, b = x
Alternatively for a 2-tuple you can use the built-in function fst
and snd
回答2:
Use Seq.map
and fst
to get a sequence of only the first component of a tuple:
let firstOnly = mappedSeq |> Seq.map fst
来源:https://stackoverflow.com/questions/39477638/how-to-extract-data-from-iterating-a-sequence