Compare item list one in list one, python

-2

I'm new to Python, I'm just learning. I try to make a program that asks the user 5 data and store them in a list; then you have to compare if there are repeated elements and the elements that are not repeated include them in a new list to be printed.

I have little, something like this:

I have made more attempts with variations of the same and so, but it does not work for me. I tried to do it with 3 values and from there, once I got out, I increased it until I got to the 6 data. As I'm learning, I'm supposed to only use things like 'for in range' 'if' 'elif' 'or' 'and' 'else' while '; that is, the basics. But I can not quite understand how they work.

I hope you can help me, thank you in advance.

    
asked by Alimania 01.12.2018 в 05:03
source

1 answer

0

Well, it's good that you go into programming.

Solution 1

I present a small code that, although it is not the best, I think it can help you understand the logic better.

print("Hola, a continuación ingresa los 5 datos")

user_inputs = []

for i in range(1, 6):
    user_inputs.append(input("Ingresa el dato número {}: ".format(i)))

unique_user_inputs = []
for i in user_inputs:
    exists = 0
    for j in user_inputs:
        if j == i:
            if exists == 1:
                break
            else:
                exists += 1
    if (exists < 2):
        unique_user_inputs.append(i)

print("Los elementos únicos son: {}".format(unique_user_inputs))

Now I explain the code:

for i in range(1, 6):
    user_inputs.append(input("Ingresa el dato número {}: ".format(i)))

The range function receives 2 essential parameters:

  • Where it starts (starting from there, if omitted it is understood that this is 0)
  • Where it ends (ending a number before, if it is 6, ends in 5)
  • There is a third optional parameter, which is the jump, of how much it goes (1 in 1, 2 in 2 ....)

    The {} and .format , is the string interpolation , which is basically replacing {} with the parameters of format

    This for checks if there are repeated elements and the only ones, puts them to another list

    for i in user_inputs:
        exists = 0
        for j in user_inputs:
            # si el elemento es el mismo
            if j == i:
                # si el elemento ya se había encontrado antes
                if exists == 1:
                    break
                # si el elemento no ha sido encontrado
                else:
                    exists += 1
        if (exists < 2):
            unique_user_inputs.append(i)
    

    Solution 2

    Although, the code of the principle can be improved in the following way:

    print("Hola, a continuación ingresa los 5 datos")
    
    user_inputs = []
    
    for i in range(1, 6):
        temp_input = input("Ingresa el dato número {}: ".format(i))
        exists = False
        for j in user_inputs:
            if j == temp_input:
                exists = True
                break
        if not exists:
            user_inputs.append(temp_input)
    
    print("Los elementos únicos son: {}".format(user_inputs))
    

    Thus, the check of repeated elements is done within the same for used to create them, saving resources and time.

    Solution 3

    Finally, this can be further simplified by doing the following:

    print("Hola, a continuación ingresa los 5 datos")
    
    user_inputs = []
    
    for i in range(1, 6):
        user_inputs.append(input("Ingresa el dato número {}: ".format(i)))
    
    print("Los elementos únicos son: {}".format(set(user_inputs)))
    

    This last method seems to me to be the fastest, but it does not have the algorithmic part that I think is what you are looking for.

    I hope I have helped you.

        
    answered by 01.12.2018 в 07:18