当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Array transpose()用法及代码示例


Array#transpose():transpose()是一个Array类方法,该方法返回数组中元素的长度,该元素将行和列转置。

用法:Array.transpose()

参数:数组


返回:转置行和列。

示例1:

# Ruby code for transpose() method 
  
# declaring array 
a = [[18, 22], [33, 3, ], [5, 6]] 
  
# declaring array 
b = [[1, 4, 1, 1, 88, 9]] 
  
# transpose method example 
puts "transpose() method form : #{a.transpose()}\n\n"
  
puts "transpose() method form : #{b.transpose()}\n\n"

输出:

transpose() method form : [[18, 33, 5], [22, 3, 6]]

transpose() method form : [[1], [4], [1], [1], [88], [9]]

示例2:

# Ruby code for transpose() method 
  
# declaring array 
a = [["abc", "nil", "dog"]] 
  
# declaring array 
b = [["cow", "go", "dog"]] 
  
  
# transpose method example 
puts "transpose() method form : #{a.transpose()}\n\n"
  
puts "transpose() method form : #{b.transpose()}\n\n"

输出:

transpose() method form : [["abc"], ["nil"], ["dog"]]

transpose() method form : [["cow"], ["go"], ["dog"]]



相关用法


注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array transpose() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。