【问题标题】:Is there a Scala equivalent to Python's list comprehension?是否有与 Python 的列表理解等效的 Scala?
【发布时间】:2013-06-25 16:47:05
【问题描述】:

我正在将我的一些 Python 代码翻译成 Scala,我想知道是否有与 Python 的 list-comprehension 等价的功能:

[x for x in list if x!=somevalue]

基本上,如果匹配,我会尝试从列表中删除某些元素。

【问题讨论】:

标签: python scala list list-comprehension


【解决方案1】:

与 Python 列表理解最接近的类似物是

for (x <- list if x != somevalue) yield x

但既然你正在做的是过滤,你还不如只使用filter方法

list.filter(_ != somevalue)

list.filterNot(_ == somevalue)

【讨论】:

猜你喜欢
  • 2011-09-23
  • 2010-11-19
  • 2017-12-30
  • 2013-02-04
  • 2012-11-21
  • 2011-03-31
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
相关资源
最近更新 更多