我如何可以选择使用Scala和Spark阵列非连续的子集的元素呢? [英] How can I select a non-sequential subset elements from an array using Scala and Spark?

查看:230
本文介绍了我如何可以选择使用Scala和Spark阵列非连续的子集的元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,这是我会怎么做。

In Python, this is how I would do it.

>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])

这不会在斯卡拉星火壳工作:

This doesn't work in the Scala Spark shell:

scala> val indices = Array(3,2,0)
indices: Array[Int] = Array(3, 2, 0)

scala> val A = Array(10,11,12,13,14,15)
A: Array[Int] = Array(10, 11, 12, 13, 14, 15)

scala> A(indices)
<console>:28: error: type mismatch;
 found   : Array[Int]
 required: Int
              A(indices)

在foreach方法不起作用或者:

The foreach method doesn't work either:

scala> indices.foreach(println(_))
3
2
0

scala> indices.foreach(A(_))
<no output>

我要的是B的结果是:

What I want is the result of B:

scala> val B = Array(A(3),A(2),A(0))
B: Array[Int] = Array(13, 12, 10)

不过,我不想硬code这样说,这是因为我不知道是指数有多长或者什么将是它。

However, I don't want to hard code it like that because I don't know how long indices is or what would be in it.

推荐答案

我能想到的最简洁的方式是翻转你的心智模式,并把指数的第一:

The most concise way I can think of is to flip your mental model and put indices first:

indices map A

和,我可能会建议使用电梯返回一个选项

And, I would potentially suggest using lift to return an Option

indices map A.lift

这篇关于我如何可以选择使用Scala和Spark阵列非连续的子集的元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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