在 Ruby 中,如何找到元素数组之一的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44030515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Ruby how do I find the index of one of an array of elements?
提问by
In Ruby 2.4, how do I find the earliest index of an element of an array in another array? That is, if any element of an array occurs in the other array, I want to get the first index. I thought find_index might do it, but
在 Ruby 2.4 中,如何在另一个数组中找到一个数组元素的最早索引?也就是说,如果一个数组的任何元素出现在另一个数组中,我想获取第一个索引。我认为 find_index 可能会这样做,但是
2.4.0 :004 > a = ["a", "b", "c"]
=> ["a", "b", "c"]
2.4.0 :005 > a.find_index("a")
=> 0
2.4.0 :006 > a.find_index(["b", "c"])
=> nil
In the above example, I would expect to see the output "1" because the element "b" occurs at index 1 in the array "a".
在上面的示例中,我希望看到输出“1”,因为元素“b”出现在数组“a”的索引 1 处。
采纳答案by Alejandro C.
find_indextakes a single element. You could find the minimum by doing something like
find_index需要一个元素。你可以通过做类似的事情来找到最小值
a = ["a", "b", "c"]
to_find = ["b", "c"]
to_find.map {|i| a.find_index(i) } .compact.min # => 1
回答by spickermann
You can use Array#indexwith a block.
您可以Array#index与块一起使用。
a = ['a', 'b', 'c']
a.index { |x| ['b', 'c'].include?(x) }
#=> 1
Quote from the docs:
来自文档的引用:
If a block is given instead of an argument, returns the index of the first object for which the block returns true. Returns nil if no match is found.
如果给出了块而不是参数,则返回块为其返回 true 的第一个对象的索引。如果未找到匹配项,则返回 nil。
As Cary pointed out in his comment it is not the most performant algorithm to compare all elements in aagainst all elements in ['b', 'c'](this would lead to O(n*m)). Depending on the size of both arrays if might sense to build a more efficient data structure first. Using a Setinstead of an Array has some cost in creating the setupfront, but makes the comparison in the block much faster (overall O(n+m)):
正如 Cary 在他的评论中指出的那样,将所有元素 ina与in中的所有元素进行比较并不是性能最高的算法['b', 'c'](这会导致O(n*m))。取决于两个数组的大小,是否可能首先构建更有效的数据结构。使用 aSet而不是 Array 在创建set前期有一些成本,但会使块中的比较更快(总体而言O(n+m)):
require 'set'
a = ['a', 'b', 'c']
set = Set.new(['b', 'c'])
a.index { |x| set.include?(x) }
#=> 1
回答by Sebastian Palma
You can use find_indexand pass the needed value from the array:
您可以使用find_index并从数组中传递所需的值:
a = ["a", "b", "c"]
p a.find_index('a')
p a.find_index('b')
p a.find_index('c')
# => 0
# => 1
# => 2
You can use mapto get every element inside your aarray and then to get the index corresponding to each element:
您可以使用map获取a数组中的每个元素,然后获取与每个元素对应的索引:
p a.map{|e| a.find_index(e)}
#?=> [0, 1, 2]
Another possible way to handle it could be to use the Enumerable#each_with_index:
另一种可能的处理方法是使用Enumerable#each_with_index:
a.each_with_index{|e,i| puts "Element: #{e}, Index: #{i}"}
#?=> Element: a, Index: 0
#?=> Element: b, Index: 1
#?=> Element: c, Index: 2
If you want to check the indexes for each element in ["b", "c"]using the ["a", "b", "c"]array, you can map the first one, get the array values, and then use the a,b,cto check those indexes:
如果要在["b", "c"]使用["a", "b", "c"]数组时检查每个元素的索引,可以映射第一个,获取数组值,然后使用a,b,c来检查这些索引:
p ["b", "c"].map{|e| ["a", "b", "c"].find_index(e) }
#?=> [1, 2]
You can also see Array#indexand Enumerable#find_index.
您还可以看到Array#index和Enumerable#find_index。
回答by Wand Maker
You could find index of all elements of array bin the array a, and find the min index to find the index at which an element from array boccurred first in array a.
您可以找到数组b中所有数组元素的索引a,并找到最小索引以找到数组中的元素b首先出现在数组中的索引a。
Something like below:
像下面这样:
a = ["a", "b", "c"]
b = ["b", "c"]
b.map { |x| a.find_index(x) }.min
#=> 1

