简体   繁体   English

如何在数组数组中获取特定值

[英]How to get specific value within an array of arrays

I have an array (outside array) that contains three arrays (inside arrays), each of which have three elements. 我有一个数组(外部数组),包含三个数组(内部数组),每个数组有三个元素。

array = [[a, b, c], [d, e, f], [g, h, i]]

I want to select the specific inside array using an index of the outside array and then select the value within the selected inside array based off its index. 我想使用外部数组的索引选择特定的内部数组,然后根据其索引选择所选内部数组中的值。 Here is what I tried: 这是我尝试过的:

array.each_index{|i| puts "letter: #{array[i[3]]} " } 

I was hoping that would give me the following output 我希望能给我以下输出

letter: c letter: f letter: i

but instead, I get 但相反,我得到了

letter: [[a, b, c], [d, e, f], [g, h, i]]

I also tried 我也试过了

array.each_index{|i| puts "letter: #{array[i][3]} " } 

but I get the same result. 但我得到了相同的结果。 Please any suggestions are very appreciated. 请任何建议非常感谢。 I need a simple explanation. 我需要一个简单的解释。

each_index is an Enumerable which goes through all indices and performs an action on each one. each_index是一个Enumerable,它遍历所有索引并对每个索引执行操作。 When it's done it will return your original collection as it's not its job to change it. 当它完成后,它将返回您的原始集合,因为它不是它的工作来改变它。 If you want to output stuff on the screen via puts / print then each_index is fine. 如果你想通过puts / print在屏幕上输出内容,那么each_index就可以了。

If you want to create a new collection as a result of going through all the elements of an original collection, you should use map . 如果要通过浏览原始集合的所有元素来创建新集合,则应使用map

eg 例如

array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
new_array = array.map {|el| el[2]}
=> ["c", "f", "i"]

array.map iterates through array's elements so in every step |el| array.map遍历数组的元素,因此在每个步骤| el |中都是如此 is an element, not an index, as in: ['a', 'b', 'c'] in the first iteration, ['d', 'e', 'f'] in the second one and so on... 是一个元素,而不是一个索引,如:第一次迭代中的['a', 'b', 'c'] ,第二次迭代中的['d', 'e', 'f'] ,依此类推。 ..

Just pointing this out since I don't know what's the goal of what you're trying to do. 只是指出这一点,因为我不知道你想要做的目标是什么。

do it like this: 像这样做:

array.each_index{|i| puts "letter: #{array[i][2]} " } 

Since you want letter at index 2, not 3. 因为你想要索引2处的字母,而不是3。

Also array should be defined like this: 数组也应该像这样定义:

array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

You could use map like so: 您可以像这样使用map

a = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
a.map(&:last)
# => ["c", "f", "i"]

Or if you really want the puts and not the collected values: 或者,如果您真的想要看跌期权而不是收集的价值:

a.each {|v| puts "letter: #{v.last}"}

You could also use Ruby's Matrix class if there's more Matrix-y stuff you want to do: 如果你想要做更多Matrix-y的东西,你也可以使用Ruby的Matrix类:

require 'matrix'
m = Matrix[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
last_column = m.column_vectors.last
# => Vector["c", "f", "i"]

You can now use any of the Vector methods on last_column , including to_a , which will put you back in familiar territory with the last column's values. 您现在可以使用last_column上的任何Vector方法,包括to_a ,这将使您回到熟悉的领域,并使用最后一列的值。

Arrays in ruby are indexed from 0, not from 1. So: ruby中的数组从0开始索引,而不是从1索引。所以:

array.each_index{|i| puts "letter: #{array[i][2]} " }

should give you what you want. 应该给你你想要的东西。

You could try the below also: 您也可以尝试下面的内容:

p RUBY_VERSION

arr = [[1,2,3],[4,5,6],[11,12,13]]

arr.each{|x| p x; x.each_index{|i| p "Digit :: #{x[i]}" if i == 2} }

output: 输出:

"2.0.0"
[1, 2, 3]
"Digit :: 3"
[4, 5, 6]
"Digit :: 6"
[11, 12, 13]
"Digit :: 13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM