Error using append after merging two lists into one

Asked

Viewed 233 times

2

My teacher passed the following problem to be solved and translated to Python:

Consider that we have two lists of people names, sedo that these lists are already in lexicographic (alphabetical) order. We wish to join these two lists in one, maintaining the alphabetical order.

In the input data, each line contains the name of a person. On the first line is the size of the first list, followed by the names of the people on this list. Next comes another row with the size of the second list, followed by the names of the people on the second list.

To solve this, I coded:

def junta_listas(lista1,lista2):
lista3 = []
lista1 = lista1.split(' ')
lista2 = lista2.split(' ')
for nome1 in lista1:
    lista3 = lista3.append(nome1)
for nome2 in lista2:
    lista3 = lista3.append(nome2)

return lista3.sort()
a = input()
b = input()

print(junta_listas(a,b))

And the environment is returning me the following error:

lista3 = lista3.append(nome1) Attributeerror: 'Nonetype' Object has no attribute 'append'

What am I doing wrong?

3 answers

3

There are many things that should be considered in your code. In short:

  1. The indentation is wrong, which would result in syntax error in this case;
  2. Uses unnecessary repeat loops;
  3. Makes wrong assignments in lista3;
  4. Returns lista3.sort(), and the return of this method is None at all times;
  5. Code does not do what is requested;

I will not answer by saying how to solve each point separately, because it is not the objective of the community to give this type of technical support and in the documentation there is enough material to study and understand the differences between the code of the question and the solution presented here.

Consider that we have two lists of people names, sedo that these lists are already in lexicographic (alphabetical) order. We wish to join these two lists in one, maintaining the alphabetical order.

This part of the statement calls for the addition of two lists that are already in order in a third that should also be in alphabetical order. To join two lists just use the operator + and the method sort (or sorted) to classify it alphabetically:

def juntar(lista1, lista2):
    lista3 = lista1 + lista2
    lista3.sort()
    return lista3

Further reading:

In the input data, each line contains the name of a person. On the first line is the size of the first list, followed by the names of the people on this list. Next comes another row with the size of the second list, followed by the names of the people on the second list.

Attention to the excerpt On the first line is the size of the first list, followed by the names of the people on this list, that is, the names will not all be on the same line as you asked in the question, but rather each name will come in a different line depending on the quantity received. So to get the two lists of names you will need to do:

quantidade1 = int(input('Tamanho da primeira lista'))
lista1 = []

for i in range(quantidade1):
    lista1.append(input(f'Nome da {i+1}a pessoa'))

quantidade2 = int(input('Tamanho da segunda lista'))
lista2 = []

for i in range(quantidade2):
    lista2.append(input(f'Nome da {i+1}a pessoa'))

print(juntar(lista1, lista2))

You can do this in a function to make it more organized and not repeat code:

def juntar_listas(a: list, b: list) -> list:
    return sorted(a + b)

def ler_lista_de_nomes() -> list:
    quantidade = int(input('Quantidade:'))
    lista = []
    for i in range(quantidade):
        lista.append(input(f'Nome da {i+1}a pessoa'))
    return lista

lista1 = ler_lista_de_nomes()
lista2 = ler_lista_de_nomes()

lista3 = juntar_listas(lista1, lista2)

See working on Repl.it

1


Hello, The problem there are two of them is the identation more I imagine it is at the time of pasting the code that she was lost, the other is that the method .append(foo) is void so there is no need to lista = lista.append(foo) only lista.append(foo) the other is that the method sort() is also void, so there is no way to return the result of it the code looks like this.

def junta_listas(lista1,lista2):
    lista3 = []
    lista1 = lista1.split(' ')
    lista2 = lista2.split(' ')
    for nome1 in lista1:
        lista3.append(nome1)
    for nome2 in lista2:
        lista3.append(nome2)
    lista3.sort()
    return lista3

a = input()
b = input()
print(junta_listas(a,b))

1

The problem is that if you use:

lista3 = lista3.append(nome)

you are actually receiving a Nonetype object in the list3 variable. This is because the . append() method is used to increment the list3.

so just use.

lista3.append(nome)

Browser other questions tagged

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