从F#的元组列表中创建列表 [英] Make a List from a Tuple List on F#

查看:65
本文介绍了从F#的元组列表中创建列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个元组列表.为了使其更易于引用,它是一个带有x和y值的坐标.

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

让测验= [(1,34);(2,43);(3,21);(1,51);(2,98);(3,56);(1,51)]

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

我想使用test列出另一个列表,这样,如果我只想要x值为1的值,它将返回[34; 51; 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]

推荐答案

您首先需要对列表进行过滤以获取x值为1的元组,然后将结果映射为y值:

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

这将返回:

[34;51;51]

这篇关于从F#的元组列表中创建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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