-2
For a little over a week I’ve been trying to solve the following exercise: "Make a program that traverses two lists and generates a third without repeating elements." The statement in question is part of Exercise 6.3 of the book "Introduction to programming with Python" by Professor Nilo Menezes.
The idea of the problem presented is that the programmer creates an algorithm that reads two different lists and that a third list is produced based on these two, ignoring the elements repeated and/or already included in the new list. How this is an exercise that aims at the practice of programming logic using the new concepts learned, it must be resolved without the use of functions.
Because of this important detail, the resolution tabled here does not apply in any way, nor is the purpose of the exercise the same as redirect link.
Prof. Nile himself has posted resolution of this same exercise in your blog, but with a significantly different logic than what I’ve been trying to: I have chosen to add the items in List 1 and List 2 to List 3 independently by checking whether there are repeated elements from each list in List 3 and, if not, by adding. Due to this factor, my code is incomplete, having been written down to the first list, since the problem is in its respective loop.
I cannot say what is wrong: after inserting the values in the variables, the program simply stops running, without being terminated by the interpreter. I even thought it might be a syntax problem, but in this case I thought some message would be displayed and the program would be terminated correctly, right?
As I commented in a paragraph above, I believe the problem lies in the second nested loop.
Below is my algorithm:
# Faça um programa que percorra duas listas e gere uma terceira sem elementos repetidos.
lista_1 = []
lista_2 = []
op = ''
while op != 0:
op = int(input(f'Digite um termo da lista 1 (0 para parar): '))
if op != 0:
lista_1.append(op)
else:
op = ''
while op != 0:
op = int(input(f'Digite um termo da lista 2 (0 para parar): '))
if op != 0:
lista_2.append(op)
indice_lista_1 = 0
indice_lista_2 = 0
lista_3 = []
indice_lista_3 = 0
while indice_lista_1 < len(lista_1):
while indice_lista_3 < len(lista_1):
if len(lista_3) == 0:
lista_3.append(lista_1[indice_lista_1]) # len(lista_3) torna-se igual a 1
else:
if lista_1[indice_lista_1] != lista_3[indice_lista_3]:
lista_3.append(lista_1[indice_lista_1])
indice_lista_3 = indice_lista_3 + 1
indice_lista_1 = indice_lista_1 + 1
print(f'A lista 1 é igual a {lista_1}')
print(f'A lista 3 é igual a {lista_3}')
lista_3 = []
I also have another draft of an attempt of mine less current. Do not include here because I did not find it relevant, but please signal me if you find it necessary.
I couldn’t let my question score drop. Please indicate me where I can improve my doubt to make it clearer and more useful to everyone.
– Rodrigo1X
I know it’s a drill and they probably want you to do it manually, but just for the record, in Python you can use
set
, which is a structure that does not allow duplicated elements, so just doterceira = list(set(primeira + segunda))
. And to print, a more idiomatic form would befor i, n in enumerate(terceira): print(f"{i}: {n}")
– hkotsubo