Problem by immutable string ... python

2

Good, I'm doing a code and I have encountered a problem because of the immutability of a string and can not avoid it. I put the code and explained:

 def muevecoche(letramov,nveles,guarda):
     elemento = nvles[guarda]
     for poyito in range(0,len(elemento)): # Extrae los valores del string.
         cadena = elemento [poyito]
         orientacion = cadena [0]
         corx = cadena[1] 
         corx = int(corx) ; corx = corx*3-2 # Establece en una celda de la matriz 3x3 la pos x
         cory = cadena[2]
         cory= int(cory); cory = cory*3-2 # Establece en una celda de la matriz 3x3 la pos y
         longitud = cadena[3]
         longitud = int(longitud)
         longitud = longitud *3 #  Multiplicacion x 3 , casillas de long 3.

     if orientacion == 'H': # Movimiento en posición horizontal.
         anchura = corx + longitud
        altura = cory+3
        for j in range(cory,altura): # Limpio la posicion anterior.
            for h in range(corx,anchura):
                t[j][h] = ' '
        if letramov <= ord('Z'): # Movimiento recibido hacia izq...
            corx= corx-3
            cadena[1] = str(corx)
            anchura = anchura-3
        elif letramov >= ord('a'): # Movimiento hacia la derecha
            corx=corx+3     
            cadena[1] = str(corx)
            anchura = anchura+3
       for j in range(cory,altura): # Pinto dicho coche
           for h in range(corx,anchura):
              t[j][h] = 'p'

       if orientacion == 'V': # Orientacion vertical..
            anchura = corx+3
            altura= cory+longitud
            for j in range(cory,altura): #Limpio la posicion anterior.
               for h in range(corx,anchura):
                  t[j][h] = ' '
       if letramov <= ord('Z'): # Hacia arriba
            cory = cory+3
            cadena[2]=str(cory)
            altura = altura+3
       elif letramov >= ord('a'):# Hacia abajo
            cory = cory-3
            cadena[2] = str(cory)
            altura = altura-3

    for j in range(cory,altura): # Genero el coche
       for h in range(corx,anchura):
          t[j][h] = 'p'

Okay the problem is that I bring from an external file a string such that "H113":

(H -> Guidance in the matrix, horizontal H.// 1-> Coord x in matrix, 1-> & coord; in matrix, 3 length of the car ..)

Which is stored in a sublist of another list .. Okay. The problem is that the purpose of this algorithm is to move a car, and therefore the position of the car element [1] and [2] of the string x and y respectively must be updated, and the string does not allow it .. Any solution is happens to you? I discard the possibility of modifying the file because first the file is already open in reading mode as well as the levels are replayable and the modification could alter the game mode.

    
asked by oxsaulxo 24.03.2017 в 20:42
source

1 answer

1

Since strings are immutable, you should first convert it to a list.

>>> cad = 'hola'
>>> nva = list(cad)

With this you can change the elements of the list using its index:

>>> nva[0] = 'm'

And then join the list in the new chain.

>>> cad = ''.join(nva)
>>> cad
'mola'

Sure it can be improved, but that's the idea.

    
answered by 24.03.2017 / 21:55
source