打印出2D数组

时间:2014-12-05 13:31:51

标签: ruby arrays printing puts

array = Array.new(10) { Array.new(10 , 0)}

array.each { |x| print x }

打印出一行10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

如果我要将print更改为puts,我会在页面下方获得100 0

如何在没有“[]”和“,”?

的单独行上打印出每个数组

类似的东西:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

4 个答案:

答案 0 :(得分:8)

假设:

arr = Array.new(10) { (0..20).to_a.sample(10) }

然后

puts arr.map { |x| x.join(' ') }
1 9 6 15 7 19 18 3 0 12
13 20 18 15 0 3 19 1 14 16
7 16 5 3 12 19 4 9 20 10
6 10 9 1 18 17 7 19 5 15
12 3 8 16 10 5 2 18 20 6
12 9 0 18 2 11 16 8 7 15
8 9 14 19 3 16 6 20 13 17
7 19 16 14 13 6 9 2 3 5
10 17 8 15 11 2 13 14 16 7
14 9 20 17 15 3 4 2 11 19

不是非常,呃,有吸引力。对于更令人愉悦的事情,你可以很容易地做到这样的事情:

width = arr.flatten.max.to_s.size+2
  #=> 4
puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join }
   1   9   6  15   7  19  18   3   0  12
  13  20  18  15   0   3  19   1  14  16
   7  16   5   3  12  19   4   9  20  10
   6  10   9   1  18  17   7  19   5  15
  12   3   8  16  10   5   2  18  20   6
  12   9   0  18   2  11  16   8   7  15
   8   9  14  19   3  16   6  20  13  17
   7  19  16  14  13   6   9   2   3   5
  10  17   8  15  11   2  13  14  16   7
  14   9  20  17  15   3   4   2  11  19

如果屏幕上显示的列太多,您可以执行以下操作:

puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join.tinyfy }
    1   9   6  15   7  19  18   3   0  12
   13  20  18  15   0   3  19   1  14  16
    7  16   5   3  12  19   4   9  20  10
    6  10   9   1  18  17   7  19   5  15
   12   3   8  16  10   5   2  18  20   6
   12   9   0  18   2  11  16   8   7  15
    8   9  14  19   3  16   6  20  13  17
    7  19  16  14  13   6   9   2   3   5
   10  17   8  15  11   2  13  14  16   7
   14   9  20  17  15   3   4   2  11  19

答案 1 :(得分:5)

尝试join

array.each { |x|
 puts x.join(" ")
}
# prints:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

答案 2 :(得分:2)

您可能想要编写自己的方法来执行此操作。类似的东西:

def array_2D_print array
    array.each do |arr|
        arr.each do |item|
            print "#{item} "
        end
        print "\n"
    end
end

如果您只在代码中使用一次,您可能还会考虑不创建任何方法:

array.each do |arr|
    arr.each do |item|
        print "#{item} "
    end
    print "\n"
end

此解决方案的优点是比其他替代方案更容易修改,以匹配您要打印的内容。

答案 3 :(得分:1)

打印列间距

的二维数组

接受具有to_s方法的对象数组(字符串整数浮点数和布尔值...)和可选的边距宽度整数

def print_table(table, margin_width = 2)
  # the margin_width is the spaces between columns (use at least 1)

  column_widths = table.transpose.collect do |column|
    column.max do |a, b|
      a.to_s.size <=> b.to_s.size
    end.to_s.size + margin_width
  end

  puts (table.collect do |row|
    row.collect.with_index do |cell, i|
      cell.to_s.ljust(column_widths[i])
    end.join
  end)
end

注意:puts语句之后的括号是必需的,因此table.collectdo end块不会作为两个单独的参数传递给puts方法。

示例表

my_table = [
  ["1", "Animal", "Dog", "1"],
  [1, "Animal", "Cat", "2"],
  [1, "Animal", "Bird", "3"],
  [2, "Place", "USA", "1"],
  [2.5, "Place", "Other", "2"],
  [3, "Color", "Red", "a"],
  [3, "Color", "Blue", "b"],
  [3, "Some more color", "Orange", "c"],
  [4.7, "Age", "Young", "a"],
  [4, "Age", "Middle", "b"],
  [4, "Age", "Old", "c"],
  [5, "Alive", true, "y"],
  [5, "Alive", false, "n"]
]

print_table my_table

打印:

1    Animal           Dog     1
1    Animal           Cat     2
1    Animal           Bird    3
2    Place            USA     1
2.5  Place            Other   2
3    Color            Red     a
3    Color            Blue    b
3    Some more color  Orange  c
4.7  Age              Young   a
4    Age              Middle  b
4    Age              Old     c
5    Alive            true    y
5    Alive            false   n

(未着色。上面的颜色是由StackOverflow添加的。)