Nested list comprehensions in Julia

前端 未结 5 1026
无人共我
无人共我 2021-02-13 16:14

In python I can do nested list comprehensions, for instance I can flatten the following array thus:

a = [[1,2,3],[4,5,6]]
[i for arr in a for i in arr]
         


        
5条回答
  •  醉酒成梦
    2021-02-13 16:20

    You can get some mileage out of using the splat operator with the array constructor here (transposing to save space)

    julia> a = ([1,2,3],[4,5,6],[7,8,9])
    ([1,2,3],[4,5,6],[7,8,9])
    
    julia> [a...]'
    1x9 Array{Int64,2}:
     1  2  3  4  5  6  7  8  9
    

提交回复
热议问题