Home > Enterprise >  Pass data selected values ​when the keys of a dictionary match
Pass data selected values ​when the keys of a dictionary match

Time:07-08

I am trying to add some values ​​from one dictionary to another, but it is giving me an error at the moment of not finding the key.

Note: I do not make this change from the database since the queries come from different sources, so I have to do this exercise.

These are my dictionaries:

apro

{'220005124956': ('220005124956', '0460', '655', 'N', 1008, '68547', 'N'), '220004340180': ('220004340180', '0110', '621', 'N', 1008, '13001',  'N'), '220005432952': ('220005432952', '0511', '0', 'S', 1008, '66170', 'S')}

qval

{'220005124956': ('220005124956', 57, 57, 'Viable', '22/03/19', '30/01/77', 'SOLICITANTE', 4), '220001563164': ('220001563164',  0, 103, 'Viable', '28/06/19', '14/08/53','DEUDOR SOLIDARIO', 1), '220002018427': ('220002018427',  31, 134, 'Viable', '23/05/19', '07/04/61', 'SOLICITANTE', 3), '220002389190': ('220002389190',  0, 52, 'Viable', '27/03/19', '26/04/63', 'SOLICITANTE', 2), '220005432952': ('220005432952', 23, 23, 'Viable', '06/06/19', '03/11/79',  'SOLICITANTE', 3), '220002392626': ('220002392626', 15, 15, 'Viable', '23/02/21', '23/09/67', 'SOLICITANTE', 3), '220003040975': ('220003040975', 23, 153, 'Viable', '05/03/21', '06/01/58', 6, 'SOLICITANTE', 4), '220004790251': ('220004790251', 20, 20, 'Viable', '09/07/19', '01/01/54', 'SOLICITANTE', 1), '220003721210': ('220003721210', 20, 21, 'Viable', '05/12/19', '15/11/84','SOLICITANTE',  3), '220003799655': ('220003799655', 0, 0, 'Viable', '25/06/21', '20/03/72', 'SOLICITANTE', 1), '220003800434': ('220003800434', 21, 162, 'Viable', '16/11/21', '13/07/66', 'SOLICITANTE', 1), '220003851821': ('220003851821', 24, 22, 'Viable', '09/09/19', '18/10/81', 'SOLICITANTE', 2), '220003851944': ('220003851944', 8, 12, 'Viable', '27/09/19', '22/12/64','SOLICITANTE', 3), '220004430315': ('220004430315', 18, 39, 'Viable', '07/10/21', '14/02/53', 'SOLICITANTE', 1), '220004504137': ('220004504137', 40, 54, 'Viable', '19/03/19', '08/03/86','SOLICITANTE', 1), '220004510839': ('220004510839',  0, 98, 'Viable', '12/10/19', '10/02/72','SOLICITANTE', 3), '220004511887': ('220004511887', '23, 137, 'Viable', '14/01/20', '05/05/59', 'SOLICITANTE', 1)}

This is the error that generates me.

Traceback (most recent call last):
  File "\adherencia.py", line 172, in <module>
    Base_adherencia()
  File "\adherencia.py", line 151, in adherencia
    res[key].append(apro   '', '', '', '')
KeyError: '220004340180'

This is my code:

def UnirDic():
    apro = {}
    for row in pcorte: ##pcorte is the result of a sql query
        apro[row[0]] = row
      
    val = Validaciones() ##Validaciones is the result of a sql query
    qval = {}
    for row in val:
        qval[row[0]]=row
    
    res = {}   

    for key, apro_row in apro.items():        
        qval_row = qval[key] if key in qval else res[key].append(apro   [qval_row[1], qval_row[2], qval_row[4], qval_row[7]])
        res[key].append(apro   '', '', '', '')
        
    for key, apro_row in res.items():
        print(key,apro_row)

CodePudding user response:

res[key].append(x) works like this:

  1. Get the value corresponding to key from dictionary res

  2. Call the .append method on the result of (1)

Now, res starts off as an empty dictionary with no keys. So step 1 is going to fail because key isn't present.

One solution is to replace the problematic line with:

res.setdefault(key, []).append((apro   '', '', '', ''))

Note, as well as the problem with key, you had a missing set of parentheses around the argument of append.

Also, have a look at the previous line. In the else case you're trying to assign the result of append to qval_row, which probably won't do what you intend, because append returns None.

CodePudding user response:

I am not sure what exactly you are trying to achieve, I can only guess that you want to merge one dictionary to another by its keys, where both dicts have tuple data types. If that's true, it's easily achieved using this code:

d1 = {'220005124956': ('220005124956', '0460', '655', 'N', 1008, '68547', 'N'), '220004340180': ('220004340180', '0110', '621', 'N', 1008, '13001',  'N'), '220005432952': ('220005432952', '0511', '0', 'S', 1008, '66170', 'S')}
d2 = {'220005124956': ('220005124956', 57, 57, 'Viable', '22/03/19', '30/01/77', 'SOLICITANTE', 4), '220001563164': ('220001563164',  0, 103, 'Viable', '28/06/19', '14/08/53','DEUDOR SOLIDARIO', 1), '220002018427': ('220002018427',  31, 134, 'Viable', '23/05/19', '07/04/61', 'SOLICITANTE', 3), '220002389190': ('220002389190',  0, 52, 'Viable', '27/03/19', '26/04/63', 'SOLICITANTE', 2), '220005432952': ('220005432952', 23, 23, 'Viable', '06/06/19', '03/11/79',  'SOLICITANTE', 3), '220002392626': ('220002392626', 15, 15, 'Viable', '23/02/21', '23/09/67', 'SOLICITANTE', 3), '220003040975': ('220003040975', 23, 153, 'Viable', '05/03/21', '06/01/58', 6, 'SOLICITANTE', 4), '220004790251': ('220004790251', 20, 20, 'Viable', '09/07/19', '01/01/54', 'SOLICITANTE', 1), '220003721210': ('220003721210', 20, 21, 'Viable', '05/12/19', '15/11/84','SOLICITANTE',  3), '220003799655': ('220003799655', 0, 0, 'Viable', '25/06/21', '20/03/72', 'SOLICITANTE', 1), '220003800434': ('220003800434', 21, 162, 'Viable', '16/11/21', '13/07/66', 'SOLICITANTE', 1), '220003851821': ('220003851821', 24, 22, 'Viable', '09/09/19', '18/10/81', 'SOLICITANTE', 2), '220003851944': ('220003851944', 8, 12, 'Viable', '27/09/19', '22/12/64','SOLICITANTE', 3), '220004430315': ('220004430315', 18, 39, 'Viable', '07/10/21', '14/02/53', 'SOLICITANTE', 1), '220004504137': ('220004504137', 40, 54, 'Viable', '19/03/19', '08/03/86','SOLICITANTE', 1), '220004510839': ('220004510839',  0, 98, 'Viable', '12/10/19', '10/02/72','SOLICITANTE', 3), '220004511887': ('220004511887', '23', 137, 'Viable', '14/01/20', '05/05/59', 'SOLICITANTE', 1)}

# Assuming you want to add values from d1 to values from d2 and allow duplicates
for key in d1:
  if key in d2:
    d2[key] = tuple(list(d1[key])   list(d2[key]))

CodePudding user response:

you should try using defaultdict,

in your imports:

from collections import defaultdict

in UnirDic(), initialize res like this:

res = defaultdict(list)
  • Related