Scala:如何将元组元素转换为列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12392409/
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
Scala: How to convert tuple elements to lists
提问by
Suppose I have the following list of tuples:
假设我有以下元组列表:
val tuples = listOfStrings.map(string => {
val split = string.split(":")
(split(0), split(1), split(2))
})
I would like to get the split(0) in a list, split(1) in another list and so on. A simple way this could be achieved is by writing:
我想在列表中获取 split(0),在另一个列表中获取 split(1) 等等。一种简单的方法是通过编写:
list1 = tuples.map(x => x._1).toList
list2 = tuples.map(x => x._2).toList
list3 = tuples.map(x => x._3).toList
Is there a more elegant (functional) way of achieving the above without writing 3 separate statements?
在不编写 3 个单独的语句的情况下,是否有更优雅(功能)的方式来实现上述目标?
采纳答案by Régis Jean-Gilles
This will give you your result as a list of list:
这将为您提供列表列表的结果:
tuples.map{t => List(t._1, t._2, t._3)}.transpose
If you want to store them in local variables, just do:
如果要将它们存储在局部变量中,只需执行以下操作:
val List(l1,l2,l3) = tuples.map{t => List(t._1, t._2, t._3)}.transpose
UPDATE: As pointed by Blaisorblade, the standard library actually has a built-in method for this: unzip3, which is just like unzipbut for triples instead of pairs:
更新:正如 Blaisorblade 所指出的,标准库实际上有一个内置的方法:unzip3,这就像unzip但是对于三元组而不是对:
val (l1, l2, l3) = tuples.unzip3
Needless to say, you should favor this method over my hand-rolled solution above (but for tuples of arity > 3, this would still still apply).
不用说,与我上面的手动解决方案相比,您应该更喜欢这种方法(但对于 arity > 3 的元组,这仍然适用)。
回答by PTWithy
You want unzip:
你想解压:
scala> val (numbers, homonyms) = List(("one", "won"), ("two", "too")).unzip
numbers: List[java.lang.String] = List(one, two)
homonyms: List[java.lang.String] = List(won, too)
回答by Kim Stebel
If you want something that can be used for arbitrary tuple sizes:
如果你想要一些可以用于任意元组大小的东西:
val tupleSize = 3
0.until(tupleSize).toList
.map(x => (_:Product).productElement(x).asInstanceOf[String])
.map(tuples.map(_))
Obviously, this could be expressed more elegantly if you had a List of Arrays instead.
显然,如果你有一个数组列表,这可以更优雅地表达。
回答by BeniBela
You could just write the statements in a single line.
您可以将语句写在一行中。
Like
喜欢
(list1, list2, list3) = tuples.foldRight((List[String](), List[String](), List[String]()))( (a,b) => (a._1 :: b._1, a._2 :: b._2, a._3 :: b._3 ) )
回答by Kyle
I don't know about elegant but you coulddo it in one line without the intermediate step of storing the tuples. Perhaps it's a little hard to read...
我不知道优雅,但您可以在一行中完成,而无需存储元组的中间步骤。可能读起来有点难……
(for(split <- listOfStrings.map(_.split(":")))
yield List(split(0), split(1), split(2))).transpose
repl example:
复制示例:
scala> listOfStrings
res1: List[java.lang.String] = List(a:b:c, d:e:f, g:h:i)
scala> (for(split <- listOfStrings.map(_.split(":")))
| yield List(split(0), split(1), split(2))).transpose
res2: List[List[java.lang.String]] = List(List(a, d, g), List(b, e, h), List(c, f, i))

