I need to remove some items from this list PYTHON

4

I need to remove from the list ( listaCoefX ) defined below the values in this format:

lista=[[-2,+1,-1,+2],[-2,+2,-1,-0],[0,+0,+2,+2],[0,+0,+0,+4]

This I take of the code to do what I request:

  listaCoefX=['-2X1+1X2-1X3+2X4', '-2X1+2X2-1X3+0X4', '0X1+0X2+2X3-2X4','0X1+0X2+0X3+4X4']

def extraerValores(listadeEc):
    lista3=[]
    lista2=[]
    for i in listadeEc:
        print "i= ",i
        aux=list(i)
        print "aux= ", aux
        lista2.append(aux)
    for i in lista2:
        while "X" in i :
            i.remove("X")
    for i in lista2:
        i.reverse()
    j=0
    while j<len(lista2):
        valor=lista2[j]
        k=0
        while k<len(valor):
            valor.pop(k)
            k=k+2

        j=j+1
    for i in lista2:
         i.reverse()
    
asked by Joaquin Tapia Jorquera 22.11.2016 в 22:47
source

4 answers

2

As I told you in another question you are complicating your life, with regular expressions is much easier and cleaner. Another example in 2 lines (one if you do not compile the regular expression):

import re

listaCoefX=['-2X1+1X2-1X3+2X4', '-2X1+2X2-1X3+0X4', '0X1+0X2+2X3-2X4','0X1+0X2+0X3+4X4']

patt = re.compile('([+-]?[\d]*)(X[\d]*)')
res = [[int(coef[0]) for coef in re.findall(patt, ecuacion)] for ecuacion in listaCoefX]

print res

This already gives you the matrix with the coefficients passed to integers:

[[-2, 1, -1, 2], [-2, 2, -1, 0], [0, 0, 2, -2], [0, 0, 0, 4]]

Update:

As @ Mariano says in the comments of this answer, it can be simplified more:

import re

listaCoefX=['-2X1+1X2-1X3+2X4', '-2X1+2X2-1X3+0X4', '0X1+0X2+2X3-2X4','0X1+0X2+0X3+4X4']

patt = re.compile('([+-]?\d*)X')
res = [[int(coef) for coef in re.findall(patt, ecuacion)] for ecuacion in   listaCoefX]

print res

Changing the expression '([+-]?[\d])(X[\d])' by '([+-]?\d*)X' . The difference is that the first one (when used in the findall() method) gives us a tuple in which the first element is the coefficient and the second the variable, that is, for the first equation something like this:

[('-2', 'X1'), ('+1', 'X2'), ('-1', 'X3'), ('+2', 'X4')]

If the variables are needed later, this form may be useful, but the second one that only gives us the coefficients is simpler.

    
answered by 23.11.2016 в 00:01
1

So you can do it

import re

def extraerValores(listadeEc):
    listacoef = []
    for ecuacion in listadeEc:
        listacoef.append(re.split('X\d', ecuacion)[0:-1])
    return listacoef

I hope it serves you

    
answered by 23.11.2016 в 00:03
0

This should work

import re

listaCoefX=['-2X1+1X2-1X3+2X4', '-2X1+2X2-1X3+0X4', '0X1+0X2+2X3-2X4','0X1+0X2+0X3+4X4']

for x in [re.split("X\d", y) for y in listaCoefX]:
    print map(int, filter(None, x))
    
answered by 22.11.2016 в 23:12
0

My proposed solution is using regular expressions:

import re

regex = re.compile("(\-|\+)(\d+)|(0)")

listaCoefX=['-2X1+1X2-1X3+2X4', '-2X1+2X2-1X3+0X4', '0X1+0X2+2X3-2X4','0X1+0X2+0X3+4X4']

output = []

for l in listaCoefX :
    r = regex.findall(l)

    f1 = ''.join(map(str, (x for x in r[0])))
    f2 = ''.join(map(str, (x for x in r[1])))
    f3 = ''.join(map(str, (x for x in r[2])))
    f4 = ''.join(map(str, (x for x in r[3])))

    output.append([f1,f2,f3,f4])

print output

Generates the following output:

[['-2', '+1', '-1', '+2'], ['-2', '+2', '-1', '+0'], ['0', '+0', '+2', '-2'], ['0', '+0', '+0', '+4']]
    
answered by 22.11.2016 в 23:37