我有以下元组:(1,"3idiots",List("Action","Adventure","Horror")
我需要将其转换为以下格式的列表:
List(
(1,"3idiots","Action"),
(1,"3idiots","Adventure")
)
答案 0 :(得分:5)
要添加到之前的答案,您还可以在这种情况下使用 for-comprehension ;它可能会使事情更清楚恕我直言:
WITH updated as(
UPDATE mytable SET status = 'A'
FROM
(
SELECT id FROM mytable
WHERE status = 'B'
ORDER BY mycolumn
LIMIT 100
FOR UPDATE
) sub
LEFT JOIN jointable j USING (id)
WHERE mytable.id = sub.id
GROUP BY (mytable.id)
RETURNING mytable.id, array_agg(j.value)
)
select *
from updated
ORDER BY mycolumn
所以如果你有:
var s:String = '364219431799224845593594391459942853555190499729152736515819';
for (var i:int = 0; i < s.length; i += 2) {
var sub:String = s.charAt(i) + s.charAt(i + 1);
trace(sub);//convert
}
你会得到:
for(
(a,b,l) <- ts;
s <- l
) yield (a,b,s)
答案 1 :(得分:1)
假设您有多个这样的元组:
val tuples = List(
(1, "3idiots", List("Action", "Adventure", "Horror")),
(2, "foobar", List("Foo", "Bar"))
)
你想要这样的结果:
List(
(1, "3idiots", "Action"),
(1, "3idiots" , "Adventure"),
(1, "3idiots", "Horror"),
(2, "foobar", "Foo"),
(2, "foobar", "Bar")
)
您的解决方案是使用flatMap
,它可以将列表列表转换为单个列表:
tuples.flatMap(t =>
t._3.map(s =>
(t._1, t._2, s)
)
)
或更短:tuples.flatMap(t => t._3.map((t._1, t._2, _)))
答案 2 :(得分:1)
这应该做你想要的:
val input = (1,"3idiots",List("Action","Adventure","Horror"))
val result = input._3.map(x => (input._1,input._2,x))
// gives List((1,3idiots,Action), (1,3idiots,Adventure), (1,3idiots,Horror))
答案 3 :(得分:1)
你可以使用它。
val question = (1,"3idiots",List("Action","Adventure","Horror"))
val result = question._3.map(x=> (question._1 , question._2 ,x))