Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy tuple with modification

Tags:

julia

I have a tuple:

my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)

I would like to modify a value in this tuple. Because tuples are immutable, the obvious route doesn't work:

my_tup[:a]=50 #Raises an error, as expected

So to perform the modification, I'd like to copy the tuple and its elements while changing the target element.

My current solution is as follows:

my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)
args = keys(my_tup)
NamedTuple{args}(i!=:a ? getfield(my_tup, i) : 50 for i in args)

But this seems verbose or as though there should already be a function for it in the standard library.

Is there a better way?

like image 608
Richard Avatar asked Feb 23 '20 01:02

Richard


People also ask

Can you modify values in a tuple?

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

Can you make a copy of a tuple?

Copy a tuple. You cannot copy a list with the = sign because lists are mutables. The = sign creates a reference not a copy. Tuples are immutable therefore a = sign does not create a reference but a copy as expected.

How do you replicate a tuple?

You can use * operator to replicate a tuple a specified number of times. Slicing refers to extracting a subpart of the mentioned sequence. Tuple- slices are same as the List- slices. Tuple slice is a tuple itself.

Can a tuple be mutated?

A tuple is immutable--it will always contain the same objects. If those objects are mutable, they might themselves be mutated, but the tuple will still contain that object.

How to modify a tuple in Python?

How to modify a tuple in Python Author: Aditya Raj Last Updated: August 27, 2021 We know that tuple is an immutable data type unlike a python dictionary or a list. That means, we cannot modify a tuple by any means. But, We may need to modify a tuple. In this case, we have no other option to create a new tuple with the desired elements.

How to create a tuple with a single element?

The first tuple will contain elements from index 0 to i-1 of the original tuple. The second tuple will contain elements from index “i” to last. After that, we will create a tuple with a single element which has to be inserted into the tuple at index i.

What is the original and updated value of a tuple?

Original Tuple is: (1, 2, 3, 4, 5, 6, 7, 8) Updated tuple after deleting element at index 3 is: (1, 2, 3, 5, 6, 7, 8) How to modify an element at a specific index in a tuple To modify an element at index “i” of a tuple, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple.

How to modify an element at index “i” of a tuple?

To modify an element at index “i” of a tuple, we will create two slices of the original tuple. The first slice will contain elements from index 0 to i-1 of the original tuple. The second slice will contain elements from index “i+1” to last. After that, we will update the element at index “i” by inserting the new value at the position as follows.


3 Answers

You can merge two named tuples:

julia> xs = (a = 1, b = 2, c = 3)
(a = 1, b = 2, c = 3)

julia> ys = merge(xs, (; a = 50))
(a = 50, b = 2, c = 3)
like image 170
David Varela Avatar answered Oct 19 '22 00:10

David Varela


David Varela has the correct answer, "use merge". It is an important technique.

  • To make this solution more easily available to others, here are some examples.

on creating plain Tuples (unnamed Tuples) and NamedTuples

Both Tuples and NamedTuples are constructed using parentheses:

julia> a_tuple = (1, 2)
(1, 2)

julia> a_namedtuple = (a = 1, b = 2)
(a = 1, b = 2)

If we try construct a Tuple or a NamedTuple with exactly one entry that same way, it does not work; instead the values are assigned named variables directly.

julia> not_a_tuple = ("xyz");
julia> not_a_tuple, typeof(not_a_tuple)
("xyz", String)

julia> not_a_namedtuple = (abc = "xyz");
julia> not_a_namedtuple, typeof(not_a_namedtuple)
("xyz", String)

To construct Tuples and NamedTuples that have a single value, we let Julia know that we are working with [Named]Tuples. An easy way to do this, and one that works for both kinds of Tuples, is to add a comma ',' before the closing parenthesis. For visual emphasis, I separate the commas from the values; it is not necessary to do that in your source code.

julia> a_tuple = (0.5 ,)
(0.5,)

julia> a_namedtuple = (onehalf = 0.5 ,)
(onehalf = 0.5,)

on substituting values in NamedTuples

julia> namedtuple = (a = 1, b = 2, c = 3);  # the initial NamedTuple
julia> changes_to_make = (b = 0 ,);         # the modifications intended

julia> changed_namedtuple = merge(namedtuple, changes_to_make)
(a = 1, b = 0, c = 3)
julia> namedtuple = (a = 1, b = 2, c = 3);  # the initial NamedTuple
julia> changes_to_make = (b = 0, c = 7);    # the modifications intended

julia> changed_namedtuple = merge(namedtuple, changes_to_make)
(a = 1, b = 0, c = 7)
like image 2
Jeffrey Sarnoff Avatar answered Oct 18 '22 23:10

Jeffrey Sarnoff


julia> my_tup=(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)
(a = 1, b = 2, c = 3,d = 4, e = 5, f = 6, g = 7, h = 8, i = 9)

julia> new_one = (my_tup..., a=50)
(a = 50, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9)
like image 1
张实唯 Avatar answered Oct 19 '22 00:10

张实唯