How to find the lists that have a certain desired item

Asked

Viewed 115 times

1

I would like to check if a specific item in a list is equal to "Aberta".

def mostrar():
    with open("ficheiro2.txt") as arquivo:
        i=0
        for linha in arquivo:
            linha = eval(linha)
            if linha[i][2]=="Aberta":
                i=i+1
                print (linha)

mostrar()

The file contains the following contents:

[['teste', '02-01-19', 'Aberta'],['teste2', '02-01-19', 'Aberta'],['teste3', '02-01-19', 'Fechada']]

And I wanted to check if the index item 2 of the list that is inside another list, is equal to "Aberta". Thus, it should show all lists where this item is equal to that value.

The exit I’ve gotten so far was this:

[['pedrito', '02-01-19', 'Aberta'], ['pedrito', '02-01-19', 'Aberta'], ['pedrito', '02-01-19', 'Fechada']]

But I wish it was:

[['pedrito', '02-01-19', 'Aberta'], ['pedrito', '02-01-19', 'Aberta']]
  • 1

    As I commented on your other question, this is not the best way to solve the problem. Why you stored the information in this way and used the eval? The given alternatives did not solve the problem? Why not?

  • To understand the error of your code, I recommend that you make a table test.

  • This question has already been answered, one of the answers is this one

  • Don’t use the eval() if the person sends you a file with these lines fm = windll.LoadLibrary('fmifs.dll') and fm.FormatEx(c_wchar_p(Drive), 0x0C, c_wchar_p(Format), c_wchar_p(Title), True, c_int(0), WINFUNCTYPE(c_int, c_int, c_int, c_void_p)(NULL)) you will have the hard drive deleted.

1 answer

3

You are getting a different result because the algorithm you are using does not do what you are wanting.

In the code, you check whether the index element 2, from sublist in position i, is equal to "Aberta".

if linha[i][2]=="Aberta":

However, if this is "true", instead of printing only the sublists that are "open", you print all the sublists, including those that are "closed".

linha = eval(linha)
if linha[i][2]=="Aberta":
    i=i+1
    # aqui
    print (linha)

Also, as @Woss commented (and answered the other question), instead of using the eval, you could use the function json.loads, to obtain the lists, or sublists, contained in the archive.


Finally, you don’t need all that code. You can simplify it by leaving it that way:

def mostrar(): 
    with open("ficheiro2.txt") as arquivo:
         lista = json.load(arquivo)
         print([sublista for sublista in lista if sublista[2] == "Aberta"]) 

mostrar()

That is to say:

  • We use the structure with to deal with the opening and closing of the file we want;

  • We obtain the list contained in the file through function json.load (that directly handles these file objects);

  • And, for each sublist that exists, we store, on a new list, those who possess the value "Aberta" at position 2.

Finally, we show this new list through the function print.

I hope I’ve helped!

Browser other questions tagged

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