Doubts about arrangements at PYTHON


~ Load two arrays of integers of N and M positions.   ~ It is requested to generate a program that produces the intersection between the two arrangements.

a = []
b = []
n1 = int(input("Ingrese la cantidad de elementos del primer arreglo: "))
n2 = int(input("Ingrese la cantidad de elementos del segundo arreglo: "))
for i in range(0,n1):
    a.append(int(input("Ingrese un numero para primer vector: ")))
for i in range(0,n2):
    b.append(int(input("Ingrese un numero para segundo vector: ")))

Hi, I'm starting to program in python and I get stuck in this exercise. My drawback is the intersection between the array a and b. I have to remove the elements that have in common both vectors but I have no idea how to do it. I thought about joining both arrangements in oneself and asking if there are more equal numbers but I think that the previous elements are going to take new positions, which I do not want. What condition should I ask to take out the repeated numbers?

asked by ailen 14.11.2018 в 02:15

3 answers


Assuming that a and b are your lists, you only need to go through one of them and compare if each element is in the other

inter = []
for i in a:
     if i in b:
 return Inter 
answered by 17.11.2018 в 08:37

Python offers very easy ways to solve this problem, from making use of advanced utilities such as data type set , to the most basic form that would be common for any programming language. I will try to give you some answers for each level of complexity, and you will take the one that best serves you based on your command of the language.

  • Data type set

    set (set) handles unordered collections of unique elements, that is, making sure none of this data is repeated. It offers facilities to apply to these elements set theory operations, such as union or, in your case, intersection. In this case I cast your list to a to type set , equal to your list b , I apply the intersection and re-casted to type list to get my final answer:

    respuesta = list(set(a).intersection(set(b)))

  • List comprehension

    This is called a dynamic way to create lists in python from the application of certain criteria to each element of another iterable ... but that is still for you;)

    respuesta = [elemento for elemento in a if elemento in b]

    In this way you create a list to which you add each element of the list to if that element is also in b.

  • Traditional for cycle

  • This solution does not seem to have much explanation, Python is explained by itself:

    respuesta = []
    for elemento in a:
        if elemento in b:

    Or even more basic:

    respuesta = []
    for elemento_in_a in a:
        for elemento_in_b in b:
            if elemento_in_a == elemento_in_b:
                break  # para que vaya a la siguiente iteración del primer for

    I tried to be as inclusive as possible. I hope it works for you and that you have a long time programming in this magnificent language.

    answered by 17.11.2018 в 09:34
    def superposicion(a,b):
        n1 = len(a)
        n2 = len(b)
        contador = 0
        for valor in range(n1):
            for valor1 in range(n2):
                if a[valor] == b[valor1]:
                    contador += 1
        if contador > 0:
            return True, contador
            return False

    Try this function, the contador is the number of intersections between both lists.

    If you need to compare two lists I advise you to use loops for nested to make the job much easier.

    answered by 17.11.2018 в 06:17