Pig*_*gna 6 arrays iteration multidimensional-array julia
假设我有 4x4 多维数组 A:
A = collect(reshape(1:16, 4, 4))
4×4 Array{Int64,2}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Run Code Online (Sandbox Code Playgroud)
我想逐行迭代(即[1, 5, 9, 13]首先,然后[2, 6, 10, 14],然后......)。
我该怎么做?现在我想出了以下几点:
`for row in 1:size(A, 1)
println(A[row, :])
# do something
end`
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一种更“pythonic”的方式来做到这一点:有点for line in A: for element in line: ....
我也知道 CartesianRange,但我希望在每次迭代时都有一个类似数组的行。
小智 8
可见性:现在(Julia > 1.1)使用“eachrow”
for row in eachrow(A)
println(row)
end
Run Code Online (Sandbox Code Playgroud)