Remove elements from a Python Stop List

Asked

Viewed 241 times

0

Basically I want to remove the elements from a second list based on the result of another list. I have two lists:

lista_8 = ['', '', 'Preco', '', 'Preco', '', '', 'Preco']

lista_n = ['positivo', 'positivo', 'negativo', 'positivo', 'negativo', 'positivo', 'positivo', 'negativo']

Follows the logic:

When the element of lista_8 is empty, the element of the lista_n also becomes empty, otherwise keeps the result (positive or negative).

My code is like this:

for w in lista_8:
    if lista_8[w] == 0:
        lista_n[w+1] = 0

print lista(n)

Follow error that happens:

if lista_8[w] == 0:

Typeerror: list indices must be integers or Slices, not str

  • 1

    The w represents each element of lista_8, is not the index. You will need to use the enumerate to manipulate the index. https://www.geeksforgeeks.org/enumerate-in-python/

  • Are you trying to walk the lista_8 with the for, when you try to check whether lista_8 in position w, error occurs because it expects an integer number of the position in the list.

3 answers

4


Using the enumerate it is possible to number a list making it easy to work with it.

lista_8 = ['', '', 'Preco', '', 'Preco', '', '', 'Preco']
lista_n = ['positivo', 'positivo', 'negativo', 'positivo', 'negativo', 'positivo', 'positivo', 'negativo']

for w, value in enumerate(lista_8, 0):
    #if lista_8[w] == '': funciona desta maneira também
    if value == '':
        lista_n[w] = ''

print("lista_8\n")
print(lista_8)
print("\nlista_n\n")
print(lista_n)

Execution of the code: https://onlinegdb.com/B1rJCwwnX

  • 1

    within the for you already have the variable value containing lista_8[w] - can use msma on if: if value == '':

  • @jsbueno yes true, I did not pay attention to that detail. I edited the answer.

3

Python has, in the package itertools, the function compress, that remove of an eternal object the values according to the value of another eternal.

def compress(data, selectors):
    # compress('ABCDEF', [1,0,1,0,1,1]) --> 'A' 'C' 'E' 'F'
    return (d for d, s in zip(data, selectors) if s)

I mean, to:

A = ['', '', 'Preco', '', 'Preco', '', '', 'Preco']
B = ['positivo', 'positivo', 'negativo', 'positivo', 'negativo', 'positivo', 'positivo', 'negativo']

In doing compress(B, A) the result would be ['negativo', 'negativo', 'negativo'], but that’s not exactly what we want. We can then adapt this function by creating a compress_with_replacement which, instead of removing the element, replaces with a defined value:

def compress_with_replacement(data, selectors, replace=''):
    # compress_with_replacement('ABCDEF', [1,0,1,0,1,1], '') --> 'A' '' 'C' '' 'E' 'F'
    return (d if s else replace for d, s in zip(data, selectors))

Thus, the return of compress_with_replacement(B, A) will be:

['', '', 'negativo', '', 'negativo', '', '', 'negativo']

Obs.: The return is actually a generator. The above result was obtained by converting the generator to a list.

0

Worked for the enumerate and so too, follows below:

i_temp = -1
for w in lista_8:
    i_temp = i_temp + 1
    if len(w) == 0:
        lista_n[i_temp] = 0
print(lista_8)
print(lista_n)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.