将迭代的每个元素与另一个元素的每个元素组合的Scala方法? [英] Scala method to combine each element of an iterable with each element of another?

查看:34
本文介绍了将迭代的每个元素与另一个元素的每个元素组合的Scala方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个:

val a = Array("a ","b ","c ")
val b = Array("x","y")

我想知道是否存在这样一种方法,它可以让我遍历第一个集合,并且对于其中的每个元素,遍历整个第二个集合.例如,如果我们取数组 a,我们将有 a,x,a,y,b,x,b,y,c,x,c,y.我知道 zip,但从我所见,它只适用于相同大小的集合,并且它关联来自相同位置的元素.

I would like to know if such a method exists which would let me traverse the first collection, and for each of it's elements, walk the entire second collection. For example, if we take the array a, we would have a,x,a,y,b,x,b,y,c,x,c,y. I know of zip but from what I've seen it only works on collections of the same sizes, and it associates elements from same positions.

推荐答案

我不确定一个方法",但这可以用嵌套/复合 for 来表达:>

I'm not sure of a "method", but this can be expressed with just a nested/compound for:

val a = Array("a ","b ","c ")
val b = Array("x","y")
for (a_ <- a; b_ <- b) yield (a_, b_)

res0: Array[(java.lang.String, java.lang.String)] = Array((a ,x), (a ,y), (b ,x), (b ,y), (c ,x), (c ,y))

快乐编码.

这篇关于将迭代的每个元素与另一个元素的每个元素组合的Scala方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆