Arrays 展平内部阵列

Arrays 展平内部阵列,arrays,ruby,Arrays,Ruby,我正在尝试创建一个输出以下内容的区域:[@month,@monthly\u count],以便完整的输出如下:[[“一月”,0],“二月”,0],“三月”,0],“四月”,2],“五月”,3],“六月”,19],“七月”,0],“八月”,0],“九月”,0],“十月”,0],“十一月”,0],“十二月”,0] 我的代码是: @months = [["January"],["February"],["March"],["April"],["May"],["June"],["July"],["Aug

我正在尝试创建一个输出以下内容的区域:
[@month,@monthly\u count]
,以便完整的输出如下:
[[“一月”,0],“二月”,0],“三月”,0],“四月”,2],“五月”,3],“六月”,19],“七月”,0],“八月”,0],“九月”,0],“十月”,0],“十一月”,0],“十二月”,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
我试图在迭代器中使用
array.flatte
来展平每个单独的数组,同时保持每个月的数组边界,但这不起作用。如何正确制作阵列?

展平(级别)
适合您

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

有关详细信息

请尝试执行
展平在您的代码中

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
12.0倍|

@每月活动计数[i]如果我了解您想要了解的内容,您可以从稍微简单一点的设置开始:

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
在此基础上,您可以使用以下任一行生成所需的数组(它们完全等效):

这两行都将遍历months数组,将月份名称及其索引传递到块中。代码块(被
{
}
包围的部分)返回一个仅包含月份名称和索引的数组-所有这些小数组都被分组到一个包含数组中,因为您使用的是
映射
(或
收集

然而,我无法想象为什么需要这样一个结构。如果你知道你想要的月份的索引,你可以这样得到它的名称:

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

month_name = "March"
index = months.index { |month| month == month_name }
# 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)
months中的
index
。index
将迭代所有月份,并将每个月份传递到代码块中。它希望您提供一个代码块,当您找到想要索引的对象时,该代码块将返回
true
。因此,代码块(
month==month\u name
)设置为将传入的月份名称与存储在
month\u name
变量中的名称相匹配。有关Ruby代码块的更多信息,请参阅。

您可以这样做:

month_name = "March"
index = months.index { |month| month == month_name }
# 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
方法的文档,请参阅


有关ruby中!(bang)方法的解释,请参阅。

这并没有改变迭代器中的任何内容,如
@monthly\u count[i]。展平(2)
,并给了我错误的输出
[“一月”,0,“二月”,0,“三月”,0,“四月”,2,“五月”,3,“六月”,19,“七月”,0,“八月”,0,“九月”,0,“十月”,0,“十一月”,0,“12月”,0]
当我做了
@每月\u计数时。展平(2)
在迭代器外。我会在迭代器外调整dif级别,但我真的希望在迭代器内有一些东西。你的代码有更多的上下文吗?在这部分代码之前,
@monthly\u activity\u count
@months
设置为什么?@bencopock在上面用适当的上下文编辑过,谢谢你的建议What Is
@monthly_count
?您的预期输出是什么?@virgeasault:
@monthly_count
不是一个整数数组,它是一个数组数组,其中每个内部数组只包含一个整数。同样,
@monthly
也是一个数组数组,其中每个内部数组只包含一个字符串。如果
@monthly_count
是一个整数数组,
@months
是一个字符串数组,你不需要首先将结果展平。这是有效的!使用
符号在这里是不受欢迎的吗?我很犹豫,因为有人教我在CSS中不要太多使用它,不确定这是否与“强制”相同“是的,它是强制的,但这并不意味着你永远不必使用它。基本上,它会将flatte的值赋回到调用实例中。+1
# 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)