Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate named tuples

Tags:

julia

I like the idea of NamedTuple a lot, as a middle ground between Tuple and full, user-defined composite types.

I know how to build a named tuple and access one of its fields

julia> nt = (a=1, b=2.0)
(a = 1, b = 2.0)

julia> nt.a
1

however, I don't know much more and don't even know whether it is possible to do more than that. I'm thinking about a lot of ways we can manipulate plain tuples (usually involving splatting), and wonder if some of those apply to named tuples as well. For example, how to:

  • dynamically build a NamedTuple from lists of fields and values
  • grow a NamedTuple , i.e add new field-value pairs to it
  • "update" (in an immutable sense) a field in an existing named tuple
like image 502
François Févotte Avatar asked Mar 27 '20 10:03

François Févotte


People also ask

Can named tuples be modified?

Since a named tuple is a tuple, and tuples are immutable, it is impossible to change the value of a field. In this case, we have to use another private method _replace() to replace values of the field. The _replace() method will return a new named tuple.

Can named tuples be pickled?

So, you can pickle a named tuple. The best way is to define your named tuple the same in both the code that contains the pickling and in the code that unpickles.

Is a named tuple immutable?

A Python namedtuple is Immutable Like its regular counterpart, a python namedtuple is immutable. We can't change its attributes. To prove this, we'll try changing one of the attributes of a tuple of type 'Colors'.

How do named tuples work?

Named tuples are basically easy-to-create, lightweight object types. Named tuple instances can be referenced using object-like variable dereferencing or the standard tuple syntax. They can be used similarly to struct or other common record types, except that they are immutable.


1 Answers

The NamedTupleTools package contains a lot of tools aiming at making the use of NamedTuples more straightforward. But here are a few elementary operations that can be performed on them "manually":


Creation

       # regular syntax
julia> nt = (a=1, b=2.)
(a = 1, b = 2.0)

       # empty named tuple (useful as a seed that will later grow)
julia> NamedTuple()
NamedTuple()

       # only one entry => don't forget the comma
julia> (a=1,)
(a = 1,)


Growth and "modification"

It is possible to merge two named tuples to create a new one:

julia> merge(nt, (c=3, d=4.))
(a = 1, b = 2.0, c = 3, d = 4.0)

...or to re-use an existing NamedTuple by splatting it in the creation of a new one:

julia> (; nt..., c=3, d=4.)
(a = 1, b = 2.0, c = 3, d = 4.0)

When the same field name appears multiple times, the last occurrence is kept. This allows for a form of "copy with modification":

julia> nt
(a = 1, b = 2.0)

julia> merge(nt, (b=3,))
(a = 1, b = 3)

julia> (; nt..., b=3)
(a = 1, b = 3)


Dynamic manipulations

Using field=>value pairs in the various techniques presented above allows for more dynamic manipulations:

julia> field = :c;

julia> merge(nt, [field=>1])
(a = 1, b = 2.0, c = 1)

julia> (; nt..., field=>1)
(a = 1, b = 2.0, c = 1)

The same technique can be used to build NamedTuples from existing dynamic data structures

julia> dic = Dict(:a=>1, :b=>2);
julia> (; dic...)
(a = 1, b = 2)

julia> arr = [:a=>1, :b=>2];
julia> (; arr...)
(a = 1, b = 2)


Iteration

Iterating on a NamedTuple iterates on its values:

julia> for val in nt
           println(val)
       end
1
2.0

Like all key->value structures, the keys function can be used to iterate over the fields:

julia> for field in keys(nt)
           val = nt[field]
           println("$field => $val")
       end
a => 1
b => 2.0
like image 81
François Févotte Avatar answered Oct 04 '22 18:10

François Févotte