using the 'any' function in python

0

I have 2 lists with multiple landline / cellular numbers. Example:

Lista 1: ['0169924233316','01687665544','01978553322']<br>
Lista 2:: ['0169924233316', '01788226541']<br>

Within a for, I compare the 2 lists to find if there is any number repeated, if it does not exist I put that number in a third list. Code:

if not any(lista1[0] in checkNumero for checkNumero in lista2): <br>
    lista3.append(lista1[0])

The problem is as follows. I now need to add another field that would be a date-time, causing the lists to become a list of dictionaries. Example:

Lista 1: [{'numero':'0169924233316', 'data':'2017-16-10'},{'numero':'01687665544', 'data':'2017-16-10'},{'numero':'01978553322', 'data':'2017-16-10'}]<br>
Lista 2:: [{'numero':'0169924233316', 'data':'2017-16-10'}, {'numero':'01788226541', 'data':'2017-16-10'}]


How would I use the 'any' function to compare both the dictionary num- ber and the dictionary time?

    
asked by anonymous 16.10.2017 / 13:16

2 answers

1
lista1 = [
    {'numero':'0169924233316', 'data':'2017-16-10'},
    {'numero':'01687665544', 'data':'2017-16-10'},
    {'numero':'01978553322', 'data':'2017-16-10'}]

lista2 = [{'numero':'0169924233316', 'data':'2017-16-10'}, 
        {'numero':'01788226541', 'data':'2017-16-10'}]

lista3 = []
for val in lista1:
    for val2 in lista2:
        if not any(val['numero']) in val2 and any(val['data']) in val2:
            lista3.append(
                {'numero':val['numero'],
                 'data':val['data']
                } 
            )

print(lista3)
    
16.10.2017 / 14:05
1

You can convert your dictionary to a set in order to facilitate operations if subtraction between lists of dictionaries:

from datetime import date

l1 = ['0169924233316','01687665544','01978553322']
l2 = ['0169924233316', '01788226541']

# Converte listas para dicionarios incluindo a chave 'data'
dic1 = [ { 'numero' : k, 'data' : date.today().strftime("%Y-%d-%m") } for k in l1 ]
dic2 = [ { 'numero' : k, 'data' : date.today().strftime("%Y-%d-%m") } for k in l2 ]

# Converte dicionarios para sets
s1 = (frozenset(x.iteritems()) for x in dic1)
s2 = (frozenset(x.iteritems()) for x in dic2)

# Calcula diferenca entre os sets
s3 = set(s1).difference(s2)

# Converte set para dicionario
dic3 = [dict(x) for x in s3]

# Exibe dicionarios
print "Lista1: ",  dic1
print "Lista2: ",  dic2
print "Lista3: ",  dic3

Output:

Lista1:  [{'data': '2017-16-10', 'numero': '0169924233316'}, {'data': '2017-16-10', 'numero': '01687665544'}, {'data': '2017-16-10', 'numero': '01978553322'}]
Lista2:  [{'data': '2017-16-10', 'numero': '0169924233316'}, {'data': '2017-16-10', 'numero': '01788226541'}]
Lista3:  [{'data': '2017-16-10', 'numero': '01978553322'}, {'data': '2017-16-10', 'numero': '01687665544'}]
    
16.10.2017 / 17:26