To resolve this issue we can use the following code:
from itertools import chain
a = [1, 2, 3]
b = [3, 4, 5]
c = list()
for item in chain(a, b):
if item not in c:
c.append(item)
c.sort()
print(f'\033[32mA lista resultante é: {c}')
Note that when we run this code, the repeat loop for
simultaneously traverse the two lists a
and b
, with the help of the method chain
library itertools
, checking that each item in each of the lists is contained in the list c
. Case negative, the respective value of the respective list is added to the list c
. Case positive, this value is not added to the list c
.
Finished adding all values without repeats, the list c
is ordered in ascending order and subsequently it is displayed.
Now, imagine if we wanted to perform these operations on any two lists. How could we solve it? In this case we should implement a code that can manipulate any two lists. For this we can use the following code:
from itertools import chain
a = list(map(int, input('Digite os valores da 1ª lista: ').split()))
b = list(map(int, input('Digite os valores da 2ª lista: ').split()))
c = list()
for item in chain(a, b):
if item not in c:
c.append(item)
c.sort()
print(f'\033[32mA lista resultante é: {c}')
When we executed this second code we received the following message: Digite os valores da 1ª lista:
. Right now we must type all the values of the first list, in the same line, separated for a single space and press enter
. Then we received the second message: Digite os valores da 2ª lista:
. At this point we must insert all the values from the second list in the same way that we entered in the first.
After you have inserted all values in the two lists the repeat loop will perform the same operations described in the first code.
What is the difference between the first and second code?
The first code is only able to work with exclusively the two lists previously inserted in the code. The second code is able to work with any two lists and any quantities of values - in each of them.
Thank you very much! But the term append is not yet part of my Python vocabulary either :x
– Guilherme Santana De Souza
This is how you put new elements at the bottom of the list. You can switch to
c = c + [elemento]
, but so you are generating several lists unnecessarily.– Pablo Almeida
I understand.. But I believe that this is more appropriate to my situation. Could you show me how it would be using the While function? The program should receive a value n and print the sum of all values less than n that are multiples of 3 and 5. The problem I had is that 3 and 5 have multiples in common, like 15 for example, and it should not be used twice in sum.
– Guilherme Santana De Souza
Haven’t you studied for yet? for is the most suitable repeat structure to browse lists.
– Pablo Almeida
@Guilhermesantanadesouza: tip: you only need the sum. Do not need to calculate the list of numbers...
– hugomg
I edited the question ^^
– Guilherme Santana De Souza
Yes, I’ve studied for, not directly, but I’ve used it many times.
– Guilherme Santana De Souza
Anyway, it seems to me you’re going down a more difficult path than you should. Why not open a question in the algorithms tag (language independent) describing in detail your problem and what you have tried so far to solve it?
– Pablo Almeida
I understand.. But I seek the path that uses the terms I already know. I will take your advice, thank you!
– Guilherme Santana De Souza
I asked a new question with the tag you indicated, and it was negative...
– Guilherme Santana De Souza
I’ll take a look
– Pablo Almeida
@Guilhermesantanadesouza I took the liberty of editing your question to leave it in the format most suitable to questions of algorithms. Soon the issue will air, then you look and see if you agree that you’re better that way.
– Pablo Almeida