Filter python list

Asked

Viewed 297 times

1

I need to remove elements from a list whose item reference for removal is in another list:

# Esta lista foi gerada por combinação
lista original=[
    (182361, 243148, 360624, 364188), 
    (182361, 243148, 360624, 547083), 
    (182361, 243148, 360624, 486296), 
    (182361, 243148, 360624, 368820), 
    (182361, 243148, 360624, 365256), 
    (182361, 243148, 364188, 547083), 
    (182361, 243148, 364188, 486296), 
    (182361, 243148, 364188, 368820), 
    (182361, 243148, 364188, 365256), 
    (182361, 243148, 547083, 486296), 
    (182361, 243148, 547083, 368820), 
    (182361, 243148, 547083, 365256), 
    (182361, 243148, 486296, 368820), 
    (182361, 243148, 486296, 365256), 
    (182361, 243148, 368820, 365256), 
    (182361, 360624, 364188, 547083), 
    (182361, 360624, 364188, 486296), 
    (182361, 360624, 364188, 368820), 
    (182361, 360624, 364188, 365256), 
    (182361, 360624, 547083, 486296), 
    (182361, 360624, 547083, 368820), 
    (182361, 360624, 547083, 365256), 
    (182361, 360624, 486296, 368820), 
    (182361, 360624, 486296, 365256), 
    (182361, 360624, 368820, 365256), 
    (182361, 364188, 547083, 486296), 
    (182361, 364188, 547083, 368820), 
    (182361, 364188, 547083, 365256), 
    (182361, 364188, 486296, 368820), 
    (182361, 364188, 486296, 365256), 
    (182361, 364188, 368820, 365256), 
    (182361, 547083, 486296, 368820), 
    (182361, 547083, 486296, 365256), 
    (182361, 547083, 368820, 365256), 
    (182361, 486296, 368820, 365256), 
    (243148, 360624, 364188, 547083), 
    (243148, 360624, 364188, 486296), 
    (243148, 360624, 364188, 368820), 
    (243148, 360624, 364188, 365256), 
    (243148, 360624, 547083, 486296), 
    (243148, 360624, 547083, 368820), 
    (243148, 360624, 547083, 365256), 
    (243148, 360624, 486296, 368820), 
    (243148, 360624, 486296, 365256), 
    (243148, 360624, 368820, 365256), 
    (243148, 364188, 547083, 486296), 
    (243148, 364188, 547083, 368820), 
    (243148, 364188, 547083, 365256), 
    (243148, 364188, 486296, 368820), 
    (243148, 364188, 486296, 365256), 
    (243148, 364188, 368820, 365256), 
    (243148, 547083, 486296, 368820), 
    (243148, 547083, 486296, 365256), 
    (243148, 547083, 368820, 365256), 
    (243148, 486296, 368820, 365256), 
    (360624, 364188, 547083, 486296), 
    (360624, 364188, 547083, 368820), 
    (360624, 364188, 547083, 365256), 
    (360624, 364188, 486296, 368820), 
    (360624, 364188, 486296, 365256), 
    (360624, 364188, 368820, 365256), 
    (360624, 547083, 486296, 368820), 
    (360624, 547083, 486296, 365256), 
    (360624, 547083, 368820, 365256), 
    (360624, 486296, 368820, 365256), 
    (364188, 547083, 486296, 368820), 
    (364188, 547083, 486296, 365256), 
    (364188, 547083, 368820, 365256), 
    (364188, 486296, 368820, 365256), 
    (547083, 486296, 368820, 365256)
]

# pares de elementos que preciso remover da lista original, ou seja, 
# se na lista original contiver qualquer desses pares que eles sejam 
# substituidos por zero.
lista de remoção=[
    [182361, 547083], 
    [243148, 486296], 
    [360624, 368820], 
    [364188, 365256]
]

I need the output to be a list composed of elements in groups of 4 (as in the original list), whose elements do not contain elements of the removal pair reference list. Where there is the presence of a pair anywhere on the removal list, let that element be replaced by zero.

I’d like to keep it that way:

lista final =[  (182361, 243148, 360624, 364188), (0, 243148, 360624, 0),   (182361, 0, 360624, 0),  (182361, 243148, 0, 0), (182361, 243148, 360624, 365256),  (0, 243148, 364188, 0),  (182361, 0, 364188, 0), 
    (182361, 243148, 364188, 368820), (182361, 243148, 0,0), 
    (0, 0, 0, 0),  (0, 243148, 0, 368820), (0, 243148, 0, 365256), 
    (182361, 0,0, 368820),  (182361, 0,0, 365256), (182361, 243148, 368820, 365256),  (0, 360624, 364188, 0), (182361, 360624, 364188, 486296), 
    (182361, 0, 364188, 0),  (182361, 360624, 0,0), (0, 360624, 0, 486296), 
    (0, 0, 0, 0),  (0, 360624, 0, 365256), (182361, 0, 486296, 0), 
    (182361, 360624, 486296, 365256), (182361, 0, 0, 365256), 
    (0, 364188, 0, 486296),  (0, 364188, 0, 368820),  (0, 0, 0, 0), 
    (182361, 364188, 486296, 368820),  (182361, 0, 486296, 0), 
    (182361, 0, 368820, 0),  (0,0, 486296, 368820), (0,0, 486296, 365256), 
    (0,0, 368820, 365256),  (182361, 486296, 368820, 365256), (243148, 360624, 364188, 547083),  (0, 360624, 364188, 0), (243148, 0, 364188, 0), 
    (243148, 360624, 0,0),  (0, 360624, 547083, 0), (243148, 0, 547083, 0), 
    (243148, 360624, 547083, 365256),  (0, 0, 0, 0), (0, 360624, 0, 365256), 
    (243148, 0, 0, 365256),  (0, 364188, 547083, 0), (243148, 364188, 547083, 368820),  (243148, 0, 547083, 0), (0, 364188, 0, 368820), (0, 0, 0, 0), 
    (243148, 0, 368820, 0),  (0, 547083, 0, 368820),  (0, 547083, 0, 365256), 
    (243148, 547083, 368820, 365256),  (0,0, 368820, 365256), (360624, 364188, 547083, 486296), (0, 364188, 547083, 0),(360624, 0, 547083, 0), 
    (0, 364188, 486296, 0), (360624, 0, 486296, 365256), (0, 364188, 0, 365256), 
    (0, 547083, 486296, 0), (360624, 547083, 486296, 365256), (0, 547083, 0, 365256),  (0, 486296, 0, 365256), (364188, 547083, 486296, 368820), 
    (0, 547083, 486296, 0), (0, 547083, 368820, 0), (0, 486296, 368820, 0), 
    (547083, 486296, 368820, 365256)] 

This would be the final list after the exclusion of pairs of elements from the exclusion list.

  • 3

    To facilitate understanding, you could edit your answer and add an example of a pair that should be removed and show how you would like it to look after the algorithm runs.

  • See if this helps you with anything: https://answall.com/q/190078/5878

  • Thanks, I’ll take a closer look today..

1 answer

0


lista_editada = []   # cria uma nova lista vazia
nao_foram_editados = [] # outra para os que nao foram editados
for grupo in lista_original:  # para cada grupo da lista
    foi_editado = False
    for teste in lista_remocao:  # testa todos da lista_remocao
        if all(item in grupo for item in teste): 
            # Achou o par! Removendo:
            grupo = tuple(0 if i in teste else i for i in grupo) 
            foi_editado = True

    lista_editada.append(grupo) # aqui adiciona todos os grupos
    if not foi_editado: # aqui adiciona os que nao sofreram edicao
         nao_foram_editados.append(grupo) 

print(lista_editada)
print(nao_foram_editados)
  • Oi Fernando. thank you for answering,the removal list contains the pairs of values I want to remove from the original list because the pairs of values in the removal list are like unwanted combinations present in the original list.

  • Nosklo, hello there! I took a look at the link you posted ,however, I think the biggest problem will be getting the code to read the removal elements that are on a list,vi which has a form of removal based on an external element ,the integer value, but the question is how to make the program read several pairs of values(value),to delete them in the original list?: refdef filter_by_generator(list, value): .. for i in list: ... if i != value: Yield i >>> list = filter_by_generator(list, 1) >>> print(list(list)) [2, 3, 5, 7, 8, 8, 9, 15].

  • @Jurandirportela my name is nosklo ; the original list has no pairs but tuples containing 4 values each... The code I placed removes the 4 values group from the list if it contains 2 values from the other list.

  • @Jurandirportela would be nice if you edit the question and put a complete example: list of values, list of pairs to remove, and especially, the result you wanted to stay after removal - so you could understand better

  • @Jurandirportela I edited the answer

  • Wonderful nosko, thank you very much. I would like it to be possible in the future,.

  • Since I don’t know how to program I’m using a visual language called Pyno, which runs in python, so I’m learning python so I can build the blocks of code.I saw that there is pymaxima, but I don’t know how to make it work and it would be very helpful to be able to perform the calculations as much as possible and bring the results to python, since unfortunately fractions like 1/18512790 have little precision in python, and it interferes a lot in the calculation relations that I am building... Strong hug to all of you who helped me in this issue... Thank you!

  • @Jurandirportela this comment is already leaving the scope of the question, but, fractions can be accurately represented in python using the module fractions or decimal depending on what you want to do - when using floats to store numbers loses accuracy yes, but it is not a problem of python but rather the way floatare stored in memory, which affects all languages programming. Read more about this in this part of the basic tutorial: https://docs.python.org/3/tutorial/floatingpoint.html

  • All right. Thank you..

  • Hello.I wonder if it is possible in the code written by nosklo to print also the list with values that have not been zeroed? That is, the list of tuples that were not filtered by the removal list.

  • I tested the code , but I checked that of the 70 input combinations, only 60 came out, I removed the all command reappeared the 70, but I will still check the list generated in the output.

  • @Jurandirportela It was to come out the same amount of combinations, once the command append is out of the if, he will always be executed ... make sure you put the identation exactly as in my answer?

  • I’ll check it out! Thank you

  • It was this,identation,fix,is working,but it would show another edited list now only with the tuples that have not been changed by the exclusion or replacement zero routine?

  • @Jurandirportela yes, just use another list - I put an example in the answer

  • Wonderful! Thank you.

  • I’ve tried where it is?

  • @Jurandirportela man, here are comments on the answer to another question, is not the place for this... That same answer is an example of what you asked for! It creates two lists, lista_editada and nao_foram_editados, and accumulates values in them with the method .append()

Show 13 more comments

Browser other questions tagged

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