List in Python with non-repeated values without using set function?

Asked

Viewed 5,527 times

2

How can I create a list from the sum of two previous without some value appearing repeated, without using the function set?

Example:

>>>a = [1, 2, 3]
>>>b = [3, 4, 5]
>>>c = a + b
>>>print c
[1, 2, 3, 3, 4, 5]

Note: I cannot use the set function to create a set because I have not seen this term in ICC classes. I am in the loop exercises (while), but in the way I wrote the program (wrong, almost right) I didn’t use links.

If you could help me by using ties, it would be interesting

How am I doing without the while:

a = int(raw_input())
x = range(0, a, 3)
y = range(0, a, 5)
lista = x + y
soma = sum(lista) 
print soma

3 answers

3

You can do it in the most direct way:

  • Using loops, you can test if a given element of the second is in the first list (and by it at the end if it is not)
  • With another loop, you can repeat this process to insert each element from the second list in the first

The downside of the direct way is that the running time is very slow if your lists are large. More precisely, if your lists have N elements, the time to do a direct "merge" will be of the order of N2

We can shorten this time to something in the order of N*LOG(N) if we take advantage of the fact that the list elements are manageable (this is the set does behind the scenes, more or less). If you sort the two lists, you can merge them in a single pass. Of course, to do this you will have to learn to order a vector efficiently, which is a subject for another class :P

2


A way would be like this:

a = [1, 2, 3]
b = [3, 4, 5]

c = []

for elemento in a:
    c.append(elemento)

for elemento in b:
    if elemento not in c:
        c.append(elemento)

What we’re doing is, for each element of the first list, we put it on the result list. Then, for each element of the second list, if it is no longer in the result list, we put.

  • Thank you very much! But the term append is not yet part of my Python vocabulary either :x

  • 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.

  • 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.

  • Haven’t you studied for yet? for is the most suitable repeat structure to browse lists.

  • @Guilhermesantanadesouza: tip: you only need the sum. Do not need to calculate the list of numbers...

  • I edited the question ^^

  • Yes, I’ve studied for, not directly, but I’ve used it many times.

  • 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?

  • I understand.. But I seek the path that uses the terms I already know. I will take your advice, thank you!

  • I asked a new question with the tag you indicated, and it was negative...

  • I’ll take a look

  • @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.

Show 7 more comments

2

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.

Browser other questions tagged

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