Program that returns the unusual numbers of two lists (python)

Asked

Viewed 392 times

8

I’m trying to make a program, in Python, that returns a list with the unusual elements between two other lists. I made the following code:

def indice(a,b):
    ind = []
    for i in range (len(a)):
        for w in range (len(b)):
            if not (a[i] in b) and not (b[w] in a):
                ind = ind + [a[i]] + [b[w]]
    return ind

However, it eventually presents repetitions in the returned list, and when the first compared list has more elements than the second, the function returns an empty list. Can someone help me?

  • For reasons of readability of the code, it is recommended to use x not in y instead of not x in y, even if both expressions are evaluated in the same way: not y.__contains__(x).

  • on a separate note: some good reason for you to be studying Python 2? The latest version is 2010, and it will no longer have updates - the best is to proceed with Python 3.6

4 answers

7

Here’s a way equivalent to @nano’s reply:

nao_comuns = list(set(a) ^ set(b))

But in a didactic way and programming the functionality you can do without using two cycles (I commented in the code some explanations):

def nao_comuns(a,b):
    a_b = a+b # juntar as duas para percorrer todos os elementos
    nao_comuns = []
    for ele in a_b:
        if ele not in a or ele not in b: # nao existe numa delas
            nao_comuns.append(ele)
    return nao_comuns

a = range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = range(0,20,2) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print nao_comuns(a,b) # [1, 3, 5, 7, 9, 10, 12, 14, 16, 18]

DEMONSTRATION

With list compreension you can make this cycle in a row:

def nao_comuns(a,b):
    a_b = a+b # juntar as duas para percorrer todos os elementos
    return [ele for ele in a_b if ele not in a or ele not in b]

a = range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = range(0,20,2) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print nao_comuns(a,b) # [1, 3, 5, 7, 9, 10, 12, 14, 16, 18]

DEMONSTRATION

Using a Generator:

def nao_comuns(a,b):
    a_b = a+b # juntar as duas para percorrer todos os elementos
    for ele in a_b:
        if ele not in a or ele not in b: # nao existe numa delas
            yield ele

a = range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = range(0,20,2) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print list(nao_comuns(a,b)) # [1, 3, 5, 7, 9, 10, 12, 14, 16, 18]

DEMONSTRATION

4

def elementos_nao_comuns(a,b):
    return list(set(a).symmetric_difference(set(b)))

3

By the way a rather primitive solution using sets:

1) not common(a,b)= a b - a b

list((set(a) | set(b)) - (set(a) & set(b)))

2) not common(a,b)= a-b b-a

list(set(a)-set(b) | set(b)-set(a))

2

Good evening, following your logic I believe the most correct would be:

def indice(a,b):
    ind = [];
    for ea in a:
        if (not (ea in b)):
            ind.append(ea);
    for eb in b:
        if (not (eb in a)):
            ind.append(eb);
    return ind;

You scroll through item by item of each list always looking if the element does not exist at the other end.

Browser other questions tagged

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