Search an array (list list)

Asked

Viewed 119 times

3

Write a function called busca that receives a matrix (each row of the matrix has four entries, representing the information related to name, registration, sector and phone) and do a search by sector. This function must return the name, registration and telephone number of the employees of that sector.

def busca(setor):
    matriz = [["Adalberto Ferreira", "566", "Contabilidade", "(21)84564-5248"],
              ["Juliana Vasconcelos", "465", "RH", "(21)3555-4552"],
              ["Flavia Amorim", "565", "Contabilidade", "(21)2134-4845"]]
    nome = matriz[0][0], matriz[1][0], matriz[2][0]
    registro = matriz[0][1], matriz[1][1], matriz[2][1]
    setor = matriz[0][2], matriz[1][2], matriz[2][2]
    telefone = matriz[0][3], matriz[1][3], matriz[2][3]
    if setor == matriz[1][3]:
        return [nome[1][0], registro[1][1], telefone[1][3]]
    else:
        return [[nome[0][0], registro[0][1], telefone[0][3]], [nome[2][0], registro[2][1], 
                telefone[2][3]]]

I arrived at this code but it just returns a part of the string, not the full string.

2 answers

3


Do not search using fixed indexes. What if the matrix has more than 3 records? What if you have less?

Instead, make a loop through the headquarters and store the sector’s employees in a list:

def busca(setor_busca):
    matriz = [["Adalberto Ferreira", "566", "Contabilidade", "(21)84564-5248"],
              ["Juliana Vasconcelos", "465", "RH", "(21)3555-4552"],
              ["Flavia Amorim", "565", "Contabilidade", "(21)2134-4845"]]
    dados = []
    for nome, registro, setor, fone in matriz:
        if setor == setor_busca:
             dados.append([nome, registro, fone])
    return dados 

print(busca('Contabilidade'))

Note that in the for I can already assign each element of the sub-lists into variables, which makes it easier to manipulate them.

There are other problems in your code: you overwrite the value of the parameter setor, and in the return you’re actually picking a single character from each string.


If you want, you can also do comprehensilist on, much more succinct and pythonic:

def busca(setor_busca):
    matriz = [["Adalberto Ferreira", "566", "Contabilidade", "(21)84564-5248"],
              ["Juliana Vasconcelos", "465", "RH", "(21)3555-4552"],
              ["Flavia Amorim", "565", "Contabilidade", "(21)2134-4845"]]
    return [  [nome, registro, fone] for nome, registro, setor, fone in matriz  if setor == setor_busca]

Another detail is that the matrix with all the data could be created outside the function and passed as parameter, so the function becomes more generic and works for any other data:

def busca(matriz, setor_busca):
    return [  [nome, registro, fone] for nome, registro, setor, fone in matriz  if setor == setor_busca]

matriz = [["Adalberto Ferreira", "566", "Contabilidade", "(21)84564-5248"],
              ["Juliana Vasconcelos", "465", "RH", "(21)3555-4552"],
              ["Flavia Amorim", "565", "Contabilidade", "(21)2134-4845"]]
print(busca(matriz, 'Contabilidade'))

# posso reusar a função para outra matriz 
outra_matriz = # outra matriz com dados diferentes 
print(busca(outra_matriz, 'outro setor'))

-2

Just to illustrate, I took the same matrix and separated only the names of each profile, apparently, I didn’t know either, but when you open the second bracket to access the element of the matrix the interpreter as if you were slicing since you are working with strings, then the correct is to indicate that it is to take the whole string, using two points(:). I checked your code and I think you still won’t be satisfied with the result, but that’s the problem.

if setor == matriz[1][3]:
        return [nome[1][0:], registro[1][1:], telefone[1][3:]]
    else:
        return [[nome[0][0:], registro[0][1:], telefone[0][3:]], [nome[2][0:], registro[2][1:], 
                telefone[2:][3:]]]

Browser other questions tagged

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