【问题标题】:Print out 2D array打印出二维数组
【发布时间】:2015-02-03 16:33:45
【问题描述】:
array = Array.new(10) { Array.new(10 , 0)}

array.each { |x| print x }

打印出一行十个[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

【问题讨论】:

    标签: ruby arrays printing puts


    【解决方案1】:

    假设:

    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
    

    【讨论】:

    • 我喜欢你调整了列。
    • tinyfy这个方法好像很有用。
    • tinyfy 方法似乎非常有用。这是在哪里实施的?我找不到任何参考。为了这个答案,我认为解释这个方法在哪里可能很有用,因为它似乎不是内置的 String 方法
    • @engineersmnky,tinyfy 是在 Ruby 2.1.5 中引入的(在撰写本文时,它是最新的稳定版本),但最新的文档适用于 v. 2.1.4。 2.1.5 文档应该很快就会发布。
    • @SergioTulentsev 谢谢我当然明白它的目的是在任何地方都找不到记录在案的方法。
    【解决方案2】:

    试试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
    

    【讨论】:

    • 或者:puts array.map { |x| x.join(' ') }
    【解决方案3】:

    打印列间距的二维数组

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

    更新:它现在适用于不同长度的数组。

    def print_table(table, margin_width = 2)
      # the margin_width is the spaces between columns (use at least 1)
    
      column_widths = []
      table.each do |row|
        row.each.with_index do |cell, column_num|
          column_widths[column_num] = [column_widths[column_num] || 0, cell.to_s.size].max
        end
      end
    
      puts (table.collect do |row|
        row.collect.with_index do |cell, column_num|
          cell.to_s.ljust(column_widths[column_num] + margin_width)
        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"],
      [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"],
      [],
      [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     
    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            
    
    5    Alive            false   n 
    

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

    【讨论】:

    • 这太棒了!比任何其他答案都好。
    • 你有允许不同大小数组的版本吗?
    • transpose 似乎有问题。您可能需要用 nil 值填充数组以使其具有相同的长度,或者重写该代码块以避免使用转置。也许我稍后会解决它。
    • @kraftydevil 我已经更新了解决方案以支持不同大小的数组。
    【解决方案4】:

    您可能想编写自己的方法来完成它。比如:

    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
    

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

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 2015-02-05
      • 2021-12-11
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多