Make a List from a Tuple List on F#

2019-07-18 05:32发布

问题:

Lets say I have a tuple list. Just to make it easier to refer to, its a coordinates with an x and y values.

let test = [(1,34);(2,43);(3,21);(1,51);(2,98);(3,56);(1,51)]

I want to make another list using test so that if I only want value which has an x value of 1, it would return [34;51;51]

回答1:

You need to filter the list first to get tuples that have an x value of 1, then map the results to get the y value :

[(1,34);(2,43);(3,21);(1,51);(2,98);(3,56);(1,51)]
|> List.filter (fun (x,_)->x=1)
|> List.map snd

This returns :

[34;51;51]