简体   繁体   中英

Is there a Scala equivalent to Python's list comprehension?

I'm translating some of my Python code to Scala, and I was wondering if there's an equivalent to Python's list-comprehension:

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

Essentially I'm trying to remove certain elements from the list if it matches.

The closest analogue to a Python list comprehension would be

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

But since you're what you're doing is filtering, you might as well just use the filter method

list.filter(_ != somevalue)

or

list.filterNot(_ == somevalue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM