我如何提取从数组特定元素? [英] How do I extract specific elements from an array?

查看:106
本文介绍了我如何提取从数组特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个数组 A = [1,2,3,4,5,6,7,8,9,10] 和我想的一个子集这个数组 - 第1,第5和第7要素。是否有可能从这个阵列中的一个简单的方法提取这些。我的想法是这样的:

  A [0,4,6] = [1,5,7]

但是,这并不工作。

也就是有没有办法返回除规定的全部指标?例如,像

  A [-0,-4,-6] = [2,3,4,6,8,9,10]


解决方案

下面是一种方法:

  [0,4,6] .MAP {| I |一个由[i]}

If I have an array a = [1,2,3,4,5,6,7,8,9,10] and I want a subset of this array - the 1st, 5th and 7th elements. Is it possible to extract these from this array in a simple way. I was thinking something like:

a[0,4,6] = [1,5,7]

but that doesn't work.

Also is there a way to return all indices except those specified? For example, something like

a[-0,-4,-6] = [2,3,4,6,8,9,10]

解决方案

Here's one way:

[0,4,6].map{|i| a[i]}

这篇关于我如何提取从数组特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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