# Changing a string in a list

I have this huge function which work as expected

def bigM_init(A,signs,b,c,M,cond):
# lines: will be the rows of the matrix
lines = []
var=[]
# column: where to put the next nonzero entry
column = 0
# total number of columns in matrix
size = len(signs)+signs.count(">=")
for s in signs:
if s == '=':
new = column * [0] + [1] + (size - column - 1) * [0]
wen=['$a_{}$'.format({column+A.ncols()})]
column += 1
elif s == '>=':
new = column * [0] + [-1, 1] + (size - column - 2) * [0]
wen=['$\\varepsilon_{}$'.format({column+A.ncols()}),'$a_{}$'.format({column+A.ncols()+1})]
column += 2
elif s == '<=':
new = column * [0] + [1] + (size - column - 1) * [0]
wen=['$\\varepsilon_{}$'.format({column+A.ncols()})]
column += 1
lines.append(new)
var.append(wen)
mat = matrix(lines)
hr=flatten(['$x_{}$'.format({i}) for i in range(0,A.ncols())]+var)
b1=list(b)
c1=list(c)
C0=block_matrix([[A,mat]],subdivide=false)
C1=list(C0)
C2=[list(v) for v in C1]
[C2[i].append(-b1[i]) for i in range(len(C1))]
H=[v for v in hr if v.startswith('$a_')] HH=[hr.index(v) for v in H] C3=[flatten([c for c in C2 if c[i]==1]) for i in HH] C4=[v[:i] for v,i in zip(C3,HH)] C5=[v[i+1:] for v,i in zip(C3,HH)] C6=[v+[0]+w for v,w in zip(C4,C5)] C7= [[-M*_ for _ in C6[i]] for i in range(len(C6))] C8=matrix(C7) C9=list(sum(C8)) cf= c1+list(zero_vector(SR, len(new)+1)) C10=[-v+w for v,w in zip(C9,cf)] C0p=matrix([list(C0[i])+[b1[i]] for i in range(len(b1))]) C11m=block_matrix([[C0p],[-matrix(C10)]],subdivide=false) C11l=list(block_matrix([[C0p],[-matrix(C10)]],subdivide=false)) #hc=['']+list(HH)+['$z$'] base_init=[] for j in range(len(signs)) : if signs[j]==">=" : base_init.append('$a_{}$'.format({j})) if signs[j]=="=" : base_init.append('$a_{}$'.format({j})) if signs[j]=="<=" : base_init.append('$\\varepsilon_{}$'.format({j+len(H)})) hr=list(hr)+list(['$b$']) hc=['']+list(base_init)+['$z$'] t = table(C11l, header_row=hr, header_column=hc,frame=True) if cond==0: return t if cond==1: return C11m if cond==2: return hc if cond==3: return hr  It can be applied on the following exemple b=vector([8,6,4]) c=vector([5,4,2]) A=matrix(3,3, [3,2,1,4,3,-1,1,2,2]) signs1 = ['=','>=','<='] M=100 var=bigM_init(A,signs1,b,c,M,3) base=bigM_init(A,signs1,b,c,M,2) show(base)  My first problem is the following, the show(base) output seems weird [,$𝚊⎯{𝟶}$,$𝚊⎯{𝟷}$,$\𝚟𝚊𝚛𝚎𝚙𝚜𝚒𝚕𝚘𝚗⎯{𝟺}$}}$𝚣$], (look at the two brakets which must be a coma). But if you ask for show(base[i]) for$i \in {0,..., 4}$the return is correct. Now I have an other function : def base_after_pivot(base, veb, vsb): #base = base #veb = variable entrant dans la base #vsb = variable sortant de la base hc=[sub.replace(vsb, veb) for sub in base] return hc  whose object is to change the name of the elements of the base. In the following context it works base1=base_after_pivot(base, '$x_0$', '$a_{1}$') show(base1)  gives the expected output (one more time seeming weird but correct) [,$𝚊⎯{𝟶}$,$𝚊⎯{𝟷}$,$\𝚟𝚊𝚛𝚎𝚙𝚜𝚒𝚕𝚘𝚗⎯{𝟺}$}}$𝚣$]. So now I try base2=base_after_pivot(base, '$x_1$', '$\varepsilon_{4}$') show(base2)  which doesn't change the base. I hope that some on will find my mistake. edit retag close merge delete ## Comments I would recommend print(base) rather than show, at least if you're using Jupyter and when you're trying to debug. show processes LaTeX and that may hide some things. ( 2022-03-05 19:01:43 +0200 )edit ## 1 Answer Sort by » oldest newest most voted \ is a special character - you need to double it or use raw strings like: base2=base_after_pivot(base, '$x_1$', r'$\varepsilon_{4}\$')

more

Max, I have followed your advice (double it or raw strings) but there is no effect. Sorry, it works when I copied the red command. I suppose there was a non displayed character.

( 2022-03-05 17:53:48 +0200 )edit