Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namedtuple for vector or pairs should allow abstract vector #38

Open
pdeffebach opened this issue Jan 29, 2021 · 5 comments
Open

namedtuple for vector or pairs should allow abstract vector #38

pdeffebach opened this issue Jan 29, 2021 · 5 comments

Comments

@pdeffebach
Copy link
Contributor

Definitely an oversight on my part

@JeffreySarnoff
Copy link
Owner

Adding this to next version.

@pdeffebach
Copy link
Contributor Author

Ah sorry I didn't respond. The motivation was, someone had a matrix of pairs and wanted to turn it into a vector of named tuples

x = [(:a => 1) (:b => 2); (:a => 3) (:b => 4)]

and namedtuple.(eachrow(x)) failed because the "rows" in eachrow are subarrays.

@JeffreySarnoff
Copy link
Owner

That is a good example.
this works now

x = [(:a => 1) (:b => 2); (:a => 3) (:b => 4); (:a => 5) (:b => 6)]
y = [ namedtuple(x[i,:]) for i=1:size(x)[1] ]

3-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 1, b = 2)
 (a = 3, b = 4)
 (a = 5, b = 6)

@JeffreySarnoff
Copy link
Owner

JeffreySarnoff commented Feb 3, 2021

notes

julia> using NamedTupleTools

julia> x = [(:a => 1) (:b => 2); (:a => 3) (:b => 4); (:a => 5) (:b => 6)]
3×2 Matrix{Pair{Symbol, Int64}}:
 :a=>1  :b=>2
 :a=>3  :b=>4
 :a=>5  :b=>6

julia> namedtuple.(Vector.(eachrow(x)))
3-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 1, b = 2)
 (a = 3, b = 4)
 (a = 5, b = 6)

so

julia> namedtuple(x::AbstractArrray) = namedtuple(Array(x))
julia> namedtuple.( eachrow(x) )
3-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 1, b = 2)
 (a = 3, b = 4)
 (a = 5, b = 6)

@JeffreySarnoff
Copy link
Owner


julia> t = [(:a => 1) (:b => 2); (:a => 3) (:b => 4); (:a => 5) (:b => 6)];

julia> collect(NamedTuple(t[i,:]) for i=1:size(t)[1])
3-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 1, b = 2)
 (a = 3, b = 4)
 (a = 5, b = 6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants