在 Ruby 中映射两个数组的值

发布于 2024-07-04 01:40:45 字数 211 浏览 9 评论 0原文

我想知道是否有一种方法可以在 Ruby 中使用 Python 执行以下操作:

sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data))

我有两个大小相等、权重和数据相同的数组,但我似乎找不到类似于 Ruby 中的 map 的函数,reduce我有工作。

I'm wondering if there's a way to do what I can do below with Python, in Ruby:

sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data))

I have two arrays of equal sizes with the weights and data but I can't seem to find a function similar to map in Ruby, reduce I have working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谜兔 2024-07-11 01:40:45

@Michiel de Mare

您的 Ruby 1.9 示例可以进一步缩短:

weights.zip(data).map(:*).reduce(:+)

另请注意,在 Ruby 1.8 中,如果您需要 ActiveSupport(来自 Rails),您可以使用:

weights.zip(data).map(&:*).reduce(&:+)

@Michiel de Mare

Your Ruby 1.9 example can be shortened a bit further:

weights.zip(data).map(:*).reduce(:+)

Also note that in Ruby 1.8, if you require ActiveSupport (from Rails) you can use:

weights.zip(data).map(&:*).reduce(&:+)
夏天碎花小短裙 2024-07-11 01:40:45

在 Ruby 1.9 中:

weights.zip(data).map{|a,b| a*b}.reduce(:+)

在 Ruby 1.8 中:

weights.zip(data).inject(0) {|sum,(w,d)| sum + w*d }

In Ruby 1.9:

weights.zip(data).map{|a,b| a*b}.reduce(:+)

In Ruby 1.8:

weights.zip(data).inject(0) {|sum,(w,d)| sum + w*d }
我只土不豪 2024-07-11 01:40:45

Array.zip 函数对数组进行元素组合。 它不像 Python 语法那么干净,但您可以使用以下一种方法:

weights = [1, 2, 3]
data = [4, 5, 6]
result = Array.new
a.zip(b) { |x, y| result << x * y } # For just the one operation

sum = 0
a.zip(b) { |x, y| sum += x * y } # For both operations

The Array.zip function does an elementwise combination of arrays. It's not quite as clean as the Python syntax, but here's one approach you could use:

weights = [1, 2, 3]
data = [4, 5, 6]
result = Array.new
a.zip(b) { |x, y| result << x * y } # For just the one operation

sum = 0
a.zip(b) { |x, y| sum += x * y } # For both operations
感情洁癖 2024-07-11 01:40:45

Ruby 有一个 map 方法(又名 collect 方法),它可以应用于任何 Enumerable 对象。 如果 numbers 是数字数组,则 Ruby 中的以下行:

numbers.map{|x| x + 5}

相当于 Python 中的以下行:

map(lambda x: x + 5, numbers)

有关更多详细信息,请参阅 此处此处

Ruby has a map method (a.k.a. the collect method), which can be applied to any Enumerable object. If numbers is an array of numbers, the following line in Ruby:

numbers.map{|x| x + 5}

is the equivalent of the following line in Python:

map(lambda x: x + 5, numbers)

For more details, see here or here.

简单爱 2024-07-11 01:40:45

也适用于 2 个以上数组的映射替代方案:

def dot(*arrays)
  arrays.transpose.map {|vals| yield vals}
end

dot(weights,data) {|a,b| a*b} 

# OR, if you have a third array

dot(weights,data,offsets) {|a,b,c| (a*b)+c}

也可以将其添加到数组中:

class Array
  def dot
    self.transpose.map{|vals| yield vals}
  end
end

[weights,data].dot {|a,b| a*b}

#OR

[weights,data,offsets].dot {|a,b,c| (a*b)+c}

An alternative for the map that works for more than 2 arrays as well:

def dot(*arrays)
  arrays.transpose.map {|vals| yield vals}
end

dot(weights,data) {|a,b| a*b} 

# OR, if you have a third array

dot(weights,data,offsets) {|a,b,c| (a*b)+c}

This could also be added to Array:

class Array
  def dot
    self.transpose.map{|vals| yield vals}
  end
end

[weights,data].dot {|a,b| a*b}

#OR

[weights,data,offsets].dot {|a,b,c| (a*b)+c}
别把无礼当个性 2024-07-11 01:40:45
weights = [1,2,3]
data    = [10,50,30]

require 'matrix'
Vector[*weights].inner_product Vector[*data] # => 200 
weights = [1,2,3]
data    = [10,50,30]

require 'matrix'
Vector[*weights].inner_product Vector[*data] # => 200 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文