繁体   English   中英

Ruby:映射哈希

[英]Ruby: Mapping a Hash

在Python中,我可以使用列表理解功能创建一个测试哈希,并对照一系列测试进行检查。 如何在红宝石中实现同一目标? (我在运行ruby 1.9.3)

蟒蛇:

test = {x: self.investor.annual_return(x) for x in xrange(1, 6)}

Ruby(尝试):

test = Hash[(1..5).map { |x| [x, @investor.annual_return(x)] }]

您想要类似的东西:

test = {}
(1..5).map { |i| test[i] = @investor.annual_return(i) }

我认为您的Ruby代码很好,具体取决于您所运行的Ruby版本。

从...开始:

class Investor
  def annual_return(i)
    i * i
  end
end

investor = Investor.new

在Ruby 1.9+中,它将起作用:

test = Hash[ (1..5).map { |x| [x, investor.annual_return(x)] } ]
test # => {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}

但是,在1.9之前的版本中,Hash不会转换包含键/值对的数组的数组,因此我们不得不花一些心思,将嵌套的元素flatten为单个数组,然后为Hash“分解”这些元素:

test = Hash[ *(1..5).map { |x| [x, investor.annual_return(x)] }.flatten ]
test # => {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}

结果是一样的,这几天不再那么麻烦。

而且,只是为了展示Ruby在我们以这种方式构建哈希表时的作用:

(1..5).map { |x| [x, investor.annual_return(x)] }
# => [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]

(1..5).map { |x| [x, investor.annual_return(x)] }.flatten
# => [1, 1, 2, 4, 3, 9, 4, 16, 5, 25]

您经常看到:

test = (1..5).reduce({}) {|h, x| h[x] = @investor.annual_return(x); h}

但是(自Ruby 1.9起)许多人更喜欢Enumerable#each_with_object

test = (1..5).each_with_object({}) {|x, h| h[x] = @investor.annual_return(x)}

部分原因是不需要像Enumerable#reduce (又称为inject )那样将对象h返回给迭代器。

如果我正确理解您要执行的操作,则可以尝试以下操作:

{}.tap { |x| (1..5).each do |y| x[y] = @investor.annual_return(i) end }

您可以轻松地做到这一点:

(1..5).map { |x| [x, @investor.annual_return(x)] }.to_h

(文档: Array#to_h

Hash[*array]用于从平面数组( [key1, value1, key2, value2, keyN, valueN] )构造哈希,而Array#to_h用于从键-值对数组构造哈希( [ [key1, value1], [key2, value2], [keyN, valueN] ] )。

暂无
暂无

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

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