How to get the union keys of the word `a` and` b` and the value of a?

I was doing a django project about handling problems request.dataand from.cleaned_data. When a user enters only input fields, he sends a request to my server. Then the form class processes the request, except for processing the entered fields and does not return the entered fields from the built-in form fields.

This is the request data:

<QueryDict: {u'is_public': [u'True']}>

This is the cleared data from the class:

{'name': u'', 'devie_type': u'', 'is_public': True, 'serial_num': u'', 'is_online': False, 'operation_system': u''}

I know this is a type of dictionary. I hope to get their connection keys from them and the values ​​of the cleared data. I expect him to return:

{u'is_public': True}

This is my attempt:

a = {}
for k in request.data:
    if k in the_form.cleaned_data:
        a[k] = the_form.cleaned_data[k]
print a

Is it readable? or are there any built-in functions for handling a federated dictionary in python?

+4
3

( , , ) , dict s, dict :

a = {k: the_form.cleaned_data[k]
     for k in request.data.viewkeys() & the_form.cleaned_data.viewkeys()}

, , , . , dict , . Python 3 .viewkeys() .keys() ( , Python 2.7, .viewkeys()).

+3

, , . , pythonic , :

a = {
   k: the_form.cleaned_data[k]
   for k in request.data
   if k in the_form.cleaned_data
}
+1

: request_data.update(form.cleaned_data)

0

Source: https://habr.com/ru/post/1613423/


All Articles