2
I have to make a code that reads two strings of numbers, convert them into int
, store in a list and print list A (first string numbers), list B (second string numbers) and list C (common numbers in both lists). I already made the code but when I print the C list it repeats the numbers instead of just showing them once.
Example:
lista A = [1 2 1 1 1]
lista B = [1 3 4 5 6]
But on list C, instead of just printing [1]
, she prints [1, 1, 1, 1]
.
Note: I know there are functions in Python that make the intersection, but the purpose of the exercise is to do without using them.
A = []
B = []
C = []
C2 = []
A = input('A: ')
B = input('B: ')
A = A.split()
B = B.split()
for x in range(0, len(A), 1):
A[x] = int(A[x])
for x in range(0, len(B), 1):
B[x] = int(B[x])
for y in A:
for x in B:
if x == y:
C = C + [x]
[C2.append(i) for i in C]
print("A =", A)
print("B =", B)
print("C =", C2)