list 从 Scala 中的列表返回一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning an element from a List in Scala
提问by
I've recently been working on a beginner's project in Scala, and have a beginner question about Scala's Lists.
我最近一直在从事 Scala 的初学者项目,并且有一个关于 Scala 列表的初学者问题。
Say I have a list of tuples ( List[Tuple2[String, String]]
, for example). Is there a convenience method to return the first occurence of a specified tuple from the List, or is it necessary to iterate through the list by hand?
假设我有一个元组列表(List[Tuple2[String, String]]
例如)。是否有一种方便的方法可以从列表中返回指定元组的第一次出现,还是需要手动遍历列表?
回答by Binil Thomas
scala> val list = List(("A", "B", 1), ("C", "D", 1), ("E", "F", 1), ("C", "D", 2), ("G", "H", 1)) list: List[(java.lang.String, java.lang.String, Int)] = List((A,B,1), (C,D,1), (E,F,1), (C,D,2), (G,H,1)) scala> list find {e => e._1 == "C" && e._2 == "D"} res0: Option[(java.lang.String, java.lang.String, Int)] = Some((C,D,1))
回答by Tim Sullivan
回答by Daniel Spiewak
As mentioned in a previous comment, find
is probably the easiest way to do this. There are actually three different "linear search" methods in Scala's collections, each returning a slightly different value. Which one you use depends upon what you need the data for. For example, do you need an index, or do you just need a boolean true
/false
?
正如之前的评论中提到的,这find
可能是最简单的方法。Scala 的集合中实际上有三种不同的“线性搜索”方法,每种方法返回的值略有不同。您使用哪一种取决于您需要数据的目的。例如,您需要一个索引,还是只需要一个布尔值true
/ false
?
回答by sblundy
回答by sblundy
You could also do this, which doesn't require knowing the field names in the Tuple2 class--it uses pattern matching instead:
你也可以这样做,这不需要知道 Tuple2 类中的字段名称——它使用模式匹配:
list find { case (x,y,_) => x == "C" && y == "D" }
"find" is good when you know you only need one; if you want to find all matching elements you could either use "filter" or the equivalent sugary for comprehension:
当您知道您只需要一个时,“查找”就很好;如果你想找到所有匹配的元素,你可以使用“过滤器”或等效的糖来理解:
for ( (x,y,z) <- list if x == "C" && y == "D") yield (x,y,z)
回答by akauppi
Here's code that may help you.
这是可能对您有所帮助的代码。
I had a similar case, having a collection of base class entries (here, A
) out of which I wanted to find a certain derived class's node, if any (here, B
).
我有一个类似的案例,有一组基类条目(此处为A
),我想从中找到某个派生类的节点(如果有)(此处为B
)。
class A
case class B(val name: String) extends A
object TestX extends App {
val states: List[A] = List( B("aa"), new A, B("ccc") )
def findByName( name: String ): Option[B] = {
states.find{
case x: B if x.name == name => return Some(x)
case _ => false
}
None
}
println( findByName("ccc") ) // "Some(B(ccc))"
}
The important part here (for my app) is that findByName
does not return Option[A]
but Option[B]
.
这里的重要部分(对于我的应用程序)是findByName
不返回Option[A]
但Option[B]
.
You can easily modify the behaviour to return B
instead, and throw an exception if none was found. Hope this helps.
您可以轻松地修改行为以返回B
,如果没有找到则抛出异常。希望这可以帮助。
回答by elm
Consider collectFirst
which delivers Some[(String,String)]
for the first matching tuple or None
otherwise, for instance as follows,
考虑第一个匹配元组或其他情况下collectFirst
哪个交付,例如如下,Some[(String,String)]
None
xs collectFirst { case t@(a,_) if a == "existing" => t }
Some((existing,str))
scala> xs collectFirst { case t@(a,_) if a == "nonExisting" => t }
None
Using @
we bind the value of the tuple to t
so that a whole matching tuple can be collected.
使用@
我们将元组的值绑定到,t
以便可以收集整个匹配的元组。