加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala过滤器列表[Int]存在于其他元组列表中

发布时间:2020-12-16 09:07:40 所属栏目:安全 来源:网络整理
导读:我有两个列表dest(包含:x)和点(x,y) dest:List[Int] and Points:List[(Int,Int)] 我想过滤dest中的元素,如果它存在于点(x == points._1)i中 var newl:List[Int] = List()for(x-dest) if(!points.filter(_._1==x).isEmpty) newl=newl:+x 我觉得必须有一个更
我有两个列表dest(包含:x)和点(x,y)

dest:List[Int] and Points:List[(Int,Int)]

我想过滤dest中的元素,如果它存在于点(x == points._1)i中

var newl:List[Int] = List()
for(x<-dest) if(!points.filter(_._1==x).isEmpty) newl=newl:+x

我觉得必须有一个更好的简洁方式与存在但元组使它复杂.那么最好的方法是什么呢?

解决方法

这是一个简洁的方法:

val dest= List(1,2,4,5)
val points = List((1,3),(2,(3,4))
val newl = dest.filter{d => points.exists(_._1 == d)} // returns List(1,2)

以下是复杂性更好的顺序:

val dest= List(1,4))
val xs = points.map{_._1}.toSet
val newl = dest.filter(xs.contains(_))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读