How to unzip an Array?

后端 未结 1 1460
抹茶落季
抹茶落季 2021-01-01 10:02

How to unzip an Array in Ruby like this examples in Python:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>>         


        
相关标签:
1条回答
  • 2021-01-01 10:45

    Use transpose:

    > zipped = x.zip(y)
    => [[1, 4], [2, 5], [3, 6]]
    > x2, y2 = zipped.transpose
    > x2
    => [1, 2, 3]
    > y2
    => [4, 5, 6]
    
    0 讨论(0)
提交回复
热议问题