如何将命名元组数组转换为命名数组元组?
How to convert array of named tuples to named tuple of arrays?
如果我有一个命名元组数组:
[(a=1,b=1),
(a=2,b=4),
(a=3,b=9)]
我想要一个命名的数组元组:
(a=[1,2,3], b=[1,4,9])
有简单的方法吗?理想情况下以懒惰的方式或不复制。
这可以通过 LazyArrays.jl 来实现。
julia> using LazyArrays
julia> xs = fill((a=1,b=2), 1024);
julia> using BenchmarkTools
julia> @btime (a = LazyArray(@~ getproperty.($xs, :a)), b = LazyArray(@~ getproperty.($xs, :b)))
10.988 ns (2 allocations: 32 bytes)
julia> (a = LazyArray(@~ getproperty.(xs, :a)), b = LazyArray(@~ getproperty.(xs, :b))) == (a=getproperty.(xs, :a), b=getproperty.(xs, :b))
true
如果我有一个命名元组数组:
[(a=1,b=1),
(a=2,b=4),
(a=3,b=9)]
我想要一个命名的数组元组:
(a=[1,2,3], b=[1,4,9])
有简单的方法吗?理想情况下以懒惰的方式或不复制。
这可以通过 LazyArrays.jl 来实现。
julia> using LazyArrays
julia> xs = fill((a=1,b=2), 1024);
julia> using BenchmarkTools
julia> @btime (a = LazyArray(@~ getproperty.($xs, :a)), b = LazyArray(@~ getproperty.($xs, :b)))
10.988 ns (2 allocations: 32 bytes)
julia> (a = LazyArray(@~ getproperty.(xs, :a)), b = LazyArray(@~ getproperty.(xs, :b))) == (a=getproperty.(xs, :a), b=getproperty.(xs, :b))
true