如何找到数组最大值的索引?
How do I find the index of the maximum value of an array?
我尝试了这里推荐的解决方案 -- In Ruby, what is the cleanest way of obtaining the index of the largest value in an array?
array = [nil, nil, nil, nil, nil, 0.9655172413793104, nil, nil]
idx = array.each_with_index.max[1]
但我遇到了一些例外情况:
ArgumentError: comparison of Array with Array failed
from (irb):4:in `each'
from (irb):4:in `each_with_index'
from (irb):4:in `each'
from (irb):4:in `max'
from (irb):4
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/console.rb:65:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
如果你想从结果中省略nil
s,那么你可以使用:
array.index(array.compact.max)
或者如果您希望将 nil
s 视为零,则首先将它们转换为 Float
s:
array.index(array.map(&:to_f).max)
如果出现平局,这将 return 第一个 最大值的索引。您还可以使用 Array#rindex
.
获取 last 索引
def max_idx(array)
mx = array.select { |e| e.kind_of?(Numeric) }.max
mx ? array.each_index.select { |i| array[i] == mx } : nil
end
require 'bigdecimal'
max_idx [nil,3,1] #=> [1]
max_idx [nil,3.2,"cat",1] #=> [1]
max_idx [nil,3,nil,1,3] #=> [1,4]
max_idx [nil,3.2,nil,1,3.2] #=> [1,4]
max_idx [nil,Rational(3),1] #=> [1]
max_idx [nil,BigDecimal.new("3"),1] #=> [1]
max_idx [nil,nil,nil] #=> nil
我尝试了这里推荐的解决方案 -- In Ruby, what is the cleanest way of obtaining the index of the largest value in an array?
array = [nil, nil, nil, nil, nil, 0.9655172413793104, nil, nil]
idx = array.each_with_index.max[1]
但我遇到了一些例外情况:
ArgumentError: comparison of Array with Array failed
from (irb):4:in `each'
from (irb):4:in `each_with_index'
from (irb):4:in `each'
from (irb):4:in `max'
from (irb):4
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/console.rb:65:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
如果你想从结果中省略nil
s,那么你可以使用:
array.index(array.compact.max)
或者如果您希望将 nil
s 视为零,则首先将它们转换为 Float
s:
array.index(array.map(&:to_f).max)
如果出现平局,这将 return 第一个 最大值的索引。您还可以使用 Array#rindex
.
def max_idx(array)
mx = array.select { |e| e.kind_of?(Numeric) }.max
mx ? array.each_index.select { |i| array[i] == mx } : nil
end
require 'bigdecimal'
max_idx [nil,3,1] #=> [1]
max_idx [nil,3.2,"cat",1] #=> [1]
max_idx [nil,3,nil,1,3] #=> [1,4]
max_idx [nil,3.2,nil,1,3.2] #=> [1,4]
max_idx [nil,Rational(3),1] #=> [1]
max_idx [nil,BigDecimal.new("3"),1] #=> [1]
max_idx [nil,nil,nil] #=> nil