How to count the elements of a list

Asked

Viewed 400 times

2

I have a list that stores data:

[(pizzas, 'COMIDA'), (1, 'QUANTIDADE'), (chocolate, 'SABOR'), (1, 'QUANTIDADE'), (catupiry, 'SABOR'), (1, 'QUANTIDADE'), (morango, 'SABOR')]

I’d like to check what’s on my list.

Example: if you have the token COMIDA, or Quantidade, or SABOR, or both, I would like you to return to me the following exit:

COMIDA = true ou a quantidade QUANTIDADE = true ou a quantidade SABOR = true ou a quantidade

Another thing, it is possible also from the Token (Example: COMIDA), return me 'pizzas', or SABOR return chocolate, morango, catupiry?

1 answer

2


Your python list is called a dictionary, and the format is 'inverted'

a = {"aaa":111, "bbb":222, "ccc":333}

this is the format of a dictionary in python or are pairs {"key":value} what you presented there was a list of tuples , but usually if I am not mistaken these tend to be numeric values.

can also create a list of objects in python

class comida:  
def __init__(self, comidaTipo, quantidade, sabor):  
    self.comidaTipo= comidaTipo
    self.quantidade = quantidade
    self.sabor = sabor

# definiçao da lista
lista = []

# adiçao de um novo alimento
lista.append(comida('pizza',1,'chocolate'))
lista.append(comida('catupiry',8,'morango')) 


# print de true se o tipo de comida estiver la lista pode fazer return true tambem se for uma função -- pizza -- catupiry etc

for comida in lista:
     if "sua_variavel_filtro" == comida.comidaTipo:
       print("true")
  • Thank you very much ! , but in the case of the dictionary, how would I filter the elements I want? example check which keys are present and their related content??

  • the keys must always be present the content is that may not exist your key is like a way to find the content what you can do is a if inside the for I will edit the answer to help you a little more

Browser other questions tagged

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