web-dev-qa-db-fra.com

Trouver des indices d'éléments qui correspondent à une condition donnée

Étant donné un tableau, comment puis-je trouver tous les indices des éléments qui correspondent à une condition donnée?

Par exemple, si j'ai:

arr = ['x', 'o', 'x', '.', '.', 'o', 'x']

Pour trouver tous les indices où l'élément est x, je pourrais faire:

arr.each_with_index.map { |a, i| a == 'x' ? i : nil }.compact   # => [0, 2, 6]

ou

(0..arr.size-1).select { |i| arr[i] == 'x' }   # => [0, 2, 6]

Existe-t-il un meilleur moyen d'y parvenir?

59
Misha Moroshko

Ruby 1.9:

arr = ['x', 'o', 'x', '.', '.', 'o', 'x']
p arr.each_index.select{|i| arr[i] == 'x'} # =>[0, 2, 6]

Code

88
steenslag

Autrement:

arr.size.times.select {|i| arr[i] == 'x'} # => [0, 2, 6]

MODIFIER:

Je ne sais pas si cela est même nécessaire, mais les voici.

Repères:

arr = 10000000.times.map{Rand(1000)};

Benchmark.measure{arr.each_with_index.map { |a, i| a == 50 ? i : nil }.compact}
2.090000   0.120000   2.210000 (  2.205431)

Benchmark.measure{(0..arr.size-1).select { |i| arr[i] == 50 }}
1.600000   0.000000   1.600000 (  1.604543)

Benchmark.measure{arr.map.with_index {|a, i| a == 50 ? i : nil}.compact}
1.810000   0.020000   1.830000 (  1.829151)

Benchmark.measure{arr.each_index.select{|i| arr[i] == 50}}
1.590000   0.000000   1.590000 (  1.584074)

Benchmark.measure{arr.size.times.select {|i| arr[i] == 50}}
1.570000   0.000000   1.570000 (  1.574474)
24
AGS

Une légère amélioration par rapport à votre each_with_index.map ligne

arr.map.with_index {|a, i| a == 'x' ? i : nil}.compact # => [0, 2, 6]
12
Sergio Tulentsev

Cette méthode est un peu plus longue mais deux fois plus rapide

class Array
  def find_each_index find
    found, index, q = -1, -1, []
    while found
      found = self[index+1..-1].index(find)
      if found
        index = index + found + 1
        q << index
      end
    end
    q
  end
end

arr = ['x', 'o', 'x', '.', '.', 'o', 'x']
p arr.find_each_index 'x'
# [0, 2, 6]

Voici la référence d'AGS campared avec cette solution

arr = 10000000.times.map{Rand(1000)};

puts Benchmark.measure{arr.each_with_index.map { |a, i| a == 50 ? i : nil }.compact}
puts Benchmark.measure{(0..arr.size-1).select { |i| arr[i] == 50 }}
puts Benchmark.measure{arr.map.with_index {|a, i| a == 50 ? i : nil}.compact}
puts Benchmark.measure{arr.each_index.select{|i| arr[i] == 50}}
puts Benchmark.measure{arr.size.times.select {|i| arr[i] == 50}}
puts Benchmark.measure{arr.find_each_index 50}

  # 1.263000   0.031000   1.294000 (  1.267073)
  # 0.843000   0.000000   0.843000 (  0.846048)
  # 0.936000   0.015000   0.951000 (  0.962055)
  # 0.842000   0.000000   0.842000 (  0.839048)
  # 0.843000   0.000000   0.843000 (  0.843048)
  # 0.405000   0.000000   0.405000 (  0.410024)
7
peter

Je ne sais pas si vous considérez cela comme une amélioration ou non, mais utiliser (map + compact) comme filtre me semble très maladroit. J'utiliserais select, puisque c'est à ça que sert, puis je récupère juste la partie du résultat qui m'importe:

arr.each_with_index.select { |a,i| a == 'x' }.map &:last
4
Mark Reed

J'ai défini Array#index_all qui se comporte comme Array#index mais renvoie tous les indices correspondants. Cette méthode peut prendre un argument et bloquer.

class Array
  def index_all(obj = nil)
    if obj || block_given?
      proc = obj ? ->(i) { self[i] == obj } : ->(i) { yield self[i] }
      self.each_index.select(&proc)
    else
      self.each
    end
  end
end

require 'test/unit'

class TestArray < Test::Unit::TestCase
  def test_index_all
    arr = ['x', 'o', 'x', '.', '.', 'o', 'x']
    result = arr.index_all('x')
    assert_equal [0, 2, 6], result

    arr = [100, 200, 100, 300, 100, 400]
    result = arr.index_all {|n| n <= 200 }
    assert_equal [0, 1, 2, 4], result
  end
end
2
Junichi Ito