how to get the element from a list based on another list

Asked

Viewed 84 times

0

I wanted to remove a doubt I’m having with lists in Python, follow an example below:

lista1 = [2, -1 , 4, -5]  
lista2 = [1, 2, 4, 9]  

As long as the list element is > 0, I need to take the list element 1 and Lista2 at the same position, and add both in different list, someone can help me?

The expected outputs for the example are the lists [2, 4], which are the positive values in lista1, and [1, 4] which are the respective values in lista2.

  • What would be the expected result for this example?

  • novaLista1 = [2, 4] and novaLista2 = [1, 4] As the element of the list1 is positive, I take the element of list1 and Lista2 at the same position and add it in different lists

2 answers

3


First, you need to define your filter structure, to define which positions will remain in the list and which will be removed. To do this, simply create a list that has True at positions where the value in lista1 is positive and False otherwise.

filtro = [valor1 > 0 for valor1 in lista1]

This will generate the list [True, False, True, False] indicating what remains and what will come out.

Note: here it is important that filtro be a list (and not a generator), as below it will be consumed more than once (generator will not allow this).

From there just use the function itertools.compress:

from itertools import compress

nova_lista1 = list(compress(lista1, filtro))
nova_lista2 = list(compress(lista2, filtro))

Thus, nova_lista1 will be [2, 4] and nova_lista2 will be [1, 4].

The very function compress returns an eternal object, so if you don’t need all the values in memory you can use the return itself instead of generating a new list (proper).

In a much more iterative way, you could scroll through the two lists generating the new ones when the condition is satisfied:

lista1 = [2, -1 , 4, -5]  
lista2 = [1, 2, 4, 9]  

nova_lista1 = []
nova_lista2 = []

for valor1, valor2 in zip(lista1, lista2):
  if valor1 > 0:
    nova_lista1.append(valor1)
    nova_lista2.append(valor2)

What would generate the same result.

0

You can check inside a block for each element of lista1 and if the element is greater than zero, it will be added together with an element from lista2 in the same position i. See this code:

lista1 = [2, -1 , 4, -5]
lista2 = [1, 2, 4, 9]

nova_lista1 = []
nova_lista2 = []


i = 0 # Posição do elemento atual

for ele in lista1:

    # Verifica se o elemento é maior que 0
    if ele > 0:
        nova_lista1.append(ele)
        nova_lista2.append(lista2[i])
    i += 1

print(nova_lista1)
print(nova_lista2)

Just as you can use the function zip in the block for. This function obtains the elements of the same position in each iterable and groups them into tuples. This way we can replace the variable i. Example:

for ele, ele2 in zip(lista1,lista2):

    # Verifica se o elemento é maior que 0
    if ele > 0:
        nova_lista1.append(ele)
        nova_lista2.append(ele2)

Browser other questions tagged

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