Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How to obtain the names of a NamedTuple?

Tags:

julia

For example,

nt = (a=1,b="b",c=5.0)

How do I get the names of nt which are [:a,:b,:c]?

like image 317
xiaodai Avatar asked Oct 17 '25 07:10

xiaodai


1 Answers

As for any other key-value structure (like a dictionary), you can use the keys function:

julia> nt = (a=1,b="b",c=5.0)
(a = 1, b = "b", c = 5.0)

julia> keys(nt)
(:a, :b, :c)

Note that in general this returns an iterator over the keys. If you really want to materialize it collect the result:

julia> collect(keys(nt))
3-element Array{Symbol,1}:
 :a
 :b
 :c
like image 150
carstenbauer Avatar answered Oct 20 '25 18:10

carstenbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!