X
X
XoXoJl2020-08-06 13:54:31
Python
XoXoJl, 2020-08-06 13:54:31

How to join two arrays and subtract the same elements?

There is an array[1,2,2], and the second [1]
It is necessary that an array would be obtained only with twos without ones.
You need to do it with def(a, b)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey, 2020-08-06
@XoXoJl

def cross(a, b):
    return [x for x in a if x not in b]

P
PavelMos, 2020-08-06
@PavelMos

Do you need a check that duplicates can be in both the first and the second list? If necessary, you need to make two passes:

a=[1,2,3,4,4,4,1,1,3]
b=[2,2,5,10,11,3,3]
out1=[x for x in a if x not in b]
print ('Из А отсеяно то, что дублируется в  Б', out1)
out2=[x for x in b if x not in a]
print ('Из Б отсеяно то, что  дублируется в А', out2)
print ('Итого',out1+out2)
Из А отсеяны дубликаты из Б [1, 4, 4, 4, 1, 1]
Из Б отсеяны дубликаты из А [5, 10, 11]
Итого [1, 4, 4, 4, 1, 1, 5, 10, 11]

N
Nick, 2020-08-06
@c00re

def x(a,b):
    res = []
    for el in a:
        if el not in b: res.append(el)
    return res

Something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question