need to do a function similar to a search in an agenda

Asked

Viewed 51 times

0

Make a function that given as input a list with contact information and a string, if the string is in the list, must return contact information or contacts.

  • Inside the entry list, you have lists with contact information (name, phone(s), email and Instagram).
  • the output should be a list of the contact(s) information(s)
  • String can be uppercase and lowercase

example:

entrada([[’Juan Silva’,[’2199112233’, ’2133992211’], 
          ’[email protected]’, ’@juansilva91’],
         [’maria Leticia’,[’2198145233’], ”, ’@maria.leticia’],
         ['Daniela Silva',['23456677'], 
         '[email protected]','@danisilva']],'silv')

As the input string is 'Silv', the output should return the information of the contacts that has Silv in the name (as if it were a search in the mobile agenda)

    saida[[’Juan Silva’,[’2199112233’, ’2133992211’], 
           ’[email protected]’, ’@juansilva91’],
          ['Daniela Silva',['23456677'],
           '[email protected]','@danisilva']]

I’m trying with this code but it’s returning an error

    def teste(lista_contatos,string):
        soma=0
        for c in lista_contatos[0]:
            if string in lista_contatos[0]:
                soma=soma+lista_contatos[0]
        return soma
  • If the output should be a list, because it has soma = 0 and at the end you return the sum?

  • Another detail is that maybe using lists is not the best way to organize the data. Probably a dictionary or a named tuple be more appropriate

1 answer

0


Hello John you have made some mistakes in understanding the data structure and how to perform the return of this data

def busca_contato(lista_contatos, nome_pesquisa):
    contatos = list()
    for contato in lista_contatos:
        for dado_contato in contato:
            if nome_pesquisa in dado_contato:
                contatos.append(contato)
                break

    return contatos


lista_contato = [["Juan Silva",["2199112233", "2133992211"], "[email protected]", "@juansilva91"], ["maria Leticia",["2198145233"], "@maria.leticia"],['Daniela Silva',['23456677'],'[email protected]','@danisilva']]


print(busca_contato(lista_contato,"silv"))

Browser other questions tagged

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