简体   繁体   English

Scala获取列表中的所有元素小于特定值

[英]Scala get all elements in a list less than a certain value

I'm trying to get a partition of a list based on the values less than some parameter I'm passing into the function. 我试图基于小于传递给函数的某些参数的值来获取列表的分区。 I'm thinking of using the map function somehow to apply a function in order to make this new list, but I don't see how to do so: 我正在考虑使用map函数以某种方式应用函数以创建此新列表,但是我不知道如何这样做:

exampleList.map(s => s<10)

For instance here, I want to get all the elements of the list that are less than 10, but I feel like this would just return a list of booleans. 例如,在这里,我想获取列表中小于10的所有元素,但是我觉得这只会返回一个布尔值列表。 I know I can also use for comprehension with yield or maybe reduce, but I'm unsure of how to do so. 我知道我也可以用收益率或降低收益来理解,但是我不确定该怎么做。 (My Scala knowledge is limited) (我的Scala知识有限)

Thanks in advance for any help 预先感谢您的任何帮助

Use the filter method: 使用filter方法:

exampleList.filter(s => s < 10)

Using lambda syntactic sugar: 使用lambda语法糖:

exampleList.filter(_ < 10)

Using list comprehensions 使用列表推导

for (s <- exampleList; if s < 10) yield s

The Seq API is a good place to start if you want to expand your knowledge of the collection API: 如果您想扩展集合API的知识,那么Seq API是一个不错的起点:

http://www.scala-lang.org/api/current/index.html#scala.collection.Seq http://www.scala-lang.org/api/current/index.html#scala.collection.Seq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果另一个列表中元素的差异小于某个值,如何对列表的元素求和 - How to sum the elements of a list if the difference of elements in another list is less than a certain value 检查列表的所有值是否都小于某个数字(如果未将其设置为该数字) - Check if all values of a list are less than a certain number, if not set it to that number 获取列表的所有元素而无需迭代scala - Get all elements of List without iteration scala 从列表中删除小于findall之后的值的元素 - Remove elements from list less than a value after findall 如果与前一个元素的差异小于值,则删除列表中的元素 - Remove elements in a list if difference with previous element less than value 获取列表中小于特定元素的最小元素的最快方法 - Fastest way to get minimum elements in a list less than a particular element 从整数列表中,获取最接近并小于给定值的数字 - from list of integers, get number closest to and less than a given value 删除列表中少于1%和60%以上的所有元素 - Remove all elements which occur in less than 1% and more than 60% of the list 如何获取Scala中列表中多次出现的所有元素集? - How to get a set of all elements that occur multiple times in a list in Scala? 删除少于“x”行的列表元素 - Remove list elements with less than "x" rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM