我有两个数组:
A = ["a","s","p","e","n"]
V = ["a","e","i","o","u"]
我想输出一个数组,该数组显示数组A
中每个元素的索引,该数组也是V
中任何位置的元素。
换句话说:
some_function(A, V) == [0,3]
这是因为A[0]="a"
和A[3]="e"
匹配数组"a"
中的元素"e"
和V
。我怎么做?
答案 0 :(得分:1)
我将如何做:
A = ["a","s","p","e","n"]
V = ["a","e","i","o","u"]
A.each_index.select{|i| V.include? A[i]} # => [0, 3]
答案 1 :(得分:1)
如果V是一组数据(顺序无关紧要,没有重复项),并且它很大,那么通过将其转换为Set可以获得性能优势,以便include?
运行得更快Set建立在哈希上并获得O(1)检索时间:
require 'set'
A = ["a","s","p","e","n"]
V = Set.new ["a","e","i","o","u"]
A.each_index.select{|i| V.include? A[i]} # => [0, 3]
答案 2 :(得分:1)
@Arup已经回答了你的问题,我想我可能会详细说明一下。 Arup建议你这样做:
A.each_index.select{|i| V.include? A[i]}
,其中
A = ["a","s","p","e","n"]
V = ["a","e","i","o","u"]
首先,A.each_index
是什么?在IRB中尝试:
e = A.each_index # => #<Enumerator: ["a", "s", "p", "e", "n"]:each_index>
e.class # => Enumerator
e.to_a # => [0, 1, 2, 3, 4]
因此,枚举器e
是方法Enumerable#select的接收者,Enumerable
是一个混合模块,包含在几个Ruby类中,包括Enumerator
。想检查一下吗?
e.respond_to?(:select) # => true
e.respond_to?(:map) # => true
e.respond_to?(:reduce) # => true
接下来,请注意A.each_index
不依赖于A
的内容,只取决于其大小,因此我们可以将其替换为从0到A.size - 1
的任何枚举器,例如:
m = A.size
m.times.select{|i| V.include? A[i]} # => [0, 3]
0.upto(m-1).select{|i| V.include? A[i]} # => [0, 3]
我们可以确认这些是Enumerator对象:
m.times.class # => Enumerator
0.upto(m-1).class # => Enumerator
include Enumerable
为Array
,Hash
,Set
,Range
和IO
的其他主要类别(但是,自Ruby 1.9以来,不是String
),所以我们也可以这样做:
Array(0...m).select{|i| V.include? A[i]} # => [0, 3]
(0...m).select{|i| V.include? A[i]} # => [0, 3]
require 'set'
Set.new(0..m-1).select{|i| V.include? A[i]} # => [0, 3]
请注意,无论接收者的类如何,select
都会返回一个数组。大多数(但不是全部)返回集合的Enumerable方法,返回一个数组,而不管接收者的类。