Make a List from a Tuple List on F#

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:40:06

问题


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]


来源:https://stackoverflow.com/questions/53727787/make-a-list-from-a-tuple-list-on-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!