substitution in list

24 views
Skip to first unread message

cyrille piatecki

unread,
Dec 28, 2021, 9:09:35 AM12/28/21
to sage-support
First I have seen that perhaps my question has an answer in AskSagemath but currently it doesn't answer.

Suppose I have a list of variables x_1, x_2, x_3,...x_n

I have some sub-list of variables say [x_4, x_1, x_6...]. But whenever x_4, x_5,x_6 belong to this list I want the substitution x_4 ->epsilon_0, x_5 -> epsilon_1... (this is an only an example. Is there a way to do this in the same way that we have a substitution in a fonction in Sagemath? 

Nils Bruin

unread,
Dec 28, 2021, 1:03:51 PM12/28/21
to sage-support
That's a python question. See for instance,


If you scroll down, there are some suggestions there that deal with multiple replacements as well

Rogerio

unread,
Dec 28, 2021, 1:32:14 PM12/28/21
to sage-support

Besides the pure python options pointed above, if your list is filled by symbolic expressions, you can also turn it into a vector and do the substitutions straightforwardly.

Given

x = list(var('x', n=10))
ep = list(var('epsilon', n=10))

a random sublist

my_list = [x[randint(0,8)] for _ in range(6)]

and a substitution rule

subs_rule = dict(zip(x[4:], ep))

You can make

my_vec_list = vector(my_list).subs(subs_rule)

and turn back to a list

my_list2 = list(my_vec_list)
Reply all
Reply to author
Forward
0 new messages