展平内部阵列

时间:2015-06-12 06:43:47

标签: arrays ruby

我正在尝试创建一个输出以下内容的区域:[@month, @monthly_count],以便完整输出如下所示:[["January", 0], ["February", 0], ["March", 0], ["April", 2], ["May", 3], ["June", 19], ["July", 0], ["August", 0], ["September", 0], ["October", 0], ["November", 0], ["December", 0]]

我的代码是:

@months = [["January"],["February"],["March"],["April"],["May"],["June"],["July"],["August"],["September"],["October"],["November"],["December"]]
@monthly_count = [[0], [0], [0], [2], [3], [19], [0], [0], [0], [0], [0], [0]] 
@monthly_activity_count = Array.new(12){Array.new}
          i = 0
    12.times do |i|
      @monthly_activity_count[i] << @months[i]
      @monthly_activity_count[i] << @monthly_count[i]
      @monthly_activity_count[i].flatten
      i += 1
    end

但它输出:

[[["January"], [0]], [["February"], [0]], [["March"], [0]], [["April"], [2]], [["May"], [3]], [["June"], [19]], [["July"], [0]], [["August"], [0]], [["September"], [0]], [["October"], [0]], [["November"], [0]], [["December"], [0]]]

我尝试在迭代器中使用array.flatten来展平每个单独的数组,同时保持每个月的数组边界,但这不起作用。如何正确制作阵列?

5 个答案:

答案 0 :(得分:0)

from p in this.People where p.IsAlive select p 适合您。

flatten(level)

了解更多信息http://apidock.com/ruby/Array/flatten

答案 1 :(得分:0)

尝试在代码中执行flatten!

12.times do |i|
    @monthly_activity_count[i] << @months[i]
    @monthly_activity_count[i] << @monthly_count[i]
    @monthly_activity_count[i].flatten!
    i += 1
  end

答案 2 :(得分:0)

@monthly_activity_count = @months.flatten.zip(@monthly_count.flatten)

答案 3 :(得分:0)

如果我理解你想要了解的内容,你可以从一个稍微简单的设置开始:

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

从那里,您可以使用以下任一行来生成所需的数组(它们完全等效):

months.map.with_index { |month, index| [month, index] }
months.collect.with_index { |month, index| [month, index] }

这两行都会迭代你的月份数组,将月份名称及其索引传递给块。代码块(由{}包围的部分返回一个仅包含月份名称和索引的数组 - 所有这些小数组都被分组到一个包含数组中,因为您正在使用{{1 (或map)。

然而,我无法想象为什么你需要这样的结构。如果您知道所需月份的索引,可以这样命名:

collect

如果您知道月份名称并想知道其索引,您可以这样找到:

months[index]

month_name = "March" index = months.index { |month| month == month_name } 中的index将遍历所有月份,将每个月传递到代码块中。它希望您提供一个代码块,当您找到想要索引的对象时,该代码块将返回months.index。因此,设置代码块(true)以使传入的月份名称与存储在month == month_name变量中的名称相匹配。有关Ruby代码块的更多信息,请参阅this article

答案 4 :(得分:0)

你可以这样做:

# only using some of the array to make the example easier to read
@months = [["January"],["February"],["March"],["April"],["May"]]
@monthly_count = [[0], [0], [0], [2], [3]] 


## First Method

zipped = @months.zip(@monthly_count)
=> [[["January"], [0]], [["February"], [0]], [["March"], [0]], [["April"], [2]], [["May"], [3]]]

@monthly_activity_count = zipped.each { |pair| pair.flatten! }
=> [["January", 0], ["February", 0], ["March", 0], ["April", 2], ["May", 3]]

# could also do it as a one liner
@months.zip(@monthly_count).each { |pair| pair.flatten! }
# the flatten! in the line above is not bad, just a warning to help you understand 
# the result. The difference between `flatten` and `flatten!` is that `flatten`
# will create a new array to hold the result, whereas `flatten!` will 
# modify the array you call it on.


## Second Method

@months.flatten.zip(@monthly_count.flatten)
=> [["January", 0], ["February", 0], ["March", 0], ["April", 2], ["May", 3]]


## Ideal Method

# if you can get your months and monthly_count data as simple arrays, eg ["January", 
# "February", ...] then you can remove the flattens in the previous line, giving:
@months.zip(@monthly_count)

有关zip方法的文档,请参阅http://ruby-doc.org/core-1.9.3/Array.html#method-i-zip

请参阅http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist以获取解释!红宝石中的(爆炸)方法。