将行数组转换为列数组 [英] Convert array of rows to array of columns

查看:134
本文介绍了将行数组转换为列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将任何(等长)行数组转换为列数组的最优雅方法是什么?

What would be the most elegant way to turn any array of (equal length) rows into an array of columns?

例如:

[1,2,3]
[4,5,6]

# To    

[1,4]
[2,5]
[3,6]

这是我到目前为止:

grid = [
  [1,2,3]
  [4,5,6]
]

grid2 = []

for i in grid[0]
  grid2.push []

for row, y in grid
  for el, x in row
    grid2[x].push el

是否有可能会有1位班轮?

Is there maybe even a 1-liner that would do it?

推荐答案

CoffeeScript:

((row[i] for row in grid) for i in [0...grid[0].length])

这篇关于将行数组转换为列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆