Home > Enterprise >  How to check if a Dictionary CONTAINS the same items as another dictionary - Python
How to check if a Dictionary CONTAINS the same items as another dictionary - Python

Time:01-09

So I have two different dictionaries, the one is kind of like a "filter", and the other one is a list of dictionaries.

Currently what I'm doing is:

if all(item in tutor.items() for item in filters.items()):

The problem with this is, that I have a list of programs, that the tutor is capable of teaching in. It could be Maple, and or Geogebra. There are a lot of different options. The problem is, that a tutor may teach in multiple programs. So if I in the filters specify the program, Maple. I don't want it to show me the tutors that ONLY teach in Maple, but all the tutors where the program is/is not a part of the programs list.

So I somehow need to rewrite if all(item in tutor.items() for item in filters.items()): To something like if contains(item in tutor.items() for item in filters.items()): But that of course doesn't work.

tutor dictionary would look something like this:

{   'age': 34,
'age_interval': '27 ',
'car': 'No',
'course': '',
'educational_institution': 'Not set by tutor',
'email': '[email protected]',
'first_name': 'Ronald',
'fluent_danish': 'Not set by tutor',
'fluent_other': [''],
'gender': 'Not set by tutor',
'grade': '',
'gym_type': 'STX',
'has_second': False,
'hour_interval': None,
'hours': 0,
'id': 112306,
'inactive_reason': 'Jeg t▒r sgu ikke give ham forl▒b, s▒ g▒r ham inaktiv '
                   '-Elmar',
'last_name': 'Reagon Ravi Kumar',
'lat': 55.78319639999999,
'lat_alternative': 0,
'lng': 12.5151532,
'lng_alternative': 0,
'mobile_phone': ' 45 50213154',
'more_courses': 'Yes',
'programs': 'Not set by tutor',
'status': 'Inactive',
'still_gym': 'Not set by tutor',
'subjects': 'None, ',
'tutor_address': 'Elektrovej 330 K5 2800 kongens lyngby',
'tutor_amount_of_students': 0,
'tutor_gym': 'Not set by tutor',
'tutor_qualification': 'Not set by tutor',
'tutor_uni': 'G▒r ikke p▒ en videreg▒ende uddannelse'}
{   'age': 19,
    'age_interval': '18 til 20',
    'car': 'No',
    'course': '',
    'educational_institution': 'Not set by tutor',
    'email': '[email protected]',
    'first_name': 'Katrine',
    'fluent_danish': 'Not set by tutor',
    'fluent_other': [''],
    'gender': 'Not set by tutor',
    'grade': '',
    'gym_type': 'STX',
    'has_second': False,
    'hour_interval': None,
    'hours': 0,
    'id': 112356,
    'inactive_reason': 'Inaktiv fordi hun er Kathrine',
    'last_name': 'Mikha',
    'lat': 55.653212,
    'lat_alternative': 0,
    'lng': 12.296957,
    'lng_alternative': 0,
    'mobile_phone': '53200337',
    'more_courses': 'Yes',
    'programs': 'Not set by tutor',
    'status': 'Inactive',
    'still_gym': 'Not set by tutor',
    'subjects': 'None, ',
    'tutor_address': 'Taastrup Have 8 st. TH',
    'tutor_amount_of_students': 0,
    'tutor_gym': 'Not set by tutor',
    'tutor_qualification': 'Not set by tutor',
    'tutor_uni': 'G▒r ikke p▒ en videreg▒ende uddannelse'}
{   'age': 19,

And the filters, would just specify the same keys, and a value. For instance

{
"gym_type" "STX"
}

This is done through a GET request, to our API

@api.route("/test", methods=["GET"])
def validate_api_request():
    try:
        filters = request.json
        return get_matching_tutors(filters)
    except:
        return error_response(400, "Bad request: error in body")

def get_matching_tutors(filters):
    matching_tutors = []

    for tutor in tutor_list:
        if all(item in tutor.items() for item in filters.items()):
            matching_tutors.append(tutor)
    return jsonify(matching_tutors)

Let's say I specify this in the API call.

{
    "programs": [
        "Excel"
    ]
}

What I would get back, is a list of all the tutors that meet the requirement of being able to teach in Excel. But a lot of the tutors may be able to teach in Excel, and another program. But I will only get the tutors that ONLY teach in Excel. So the expected result should be something like this:

    {
        "age": 24,
        "age_interval": "24 til 26",
        "car": "Yes",
        "course": "4. prioritet (Foretrækker fysisk)",
        "educational_institution": "Københavns Universitet",
        "email": "[email protected]",
        "first_name": "Ahmed",
        "fluent_danish": "Yes",
        "fluent_other": [
            "Engelsk"
        ],
        "gender": "Mand",
        "grade": "7 til 8",
        "gym_type": "STX",
        "has_second": true,
        "hour_interval": null,
        "hours": 0,
        "id": 134781,
        "inactive_reason": "Blank",
        "last_name": "Osman Mohammed",
        "lat": 55.70321,
        "lat_alternative": 55.68784609999999,
        "lng": 12.530245,
        "lng_alternative": 12.5696519,
        "mobile_phone": "42313324",
        "more_courses": "Yes",
        "programs": [
            "TI-Nspire",
            "Geogebra",
            "Wordmat",
            "Excel",
            "STATA"
        ],
        "status": "Active",
        "still_gym": "Jeg er færdig med gymnasiet",
        "subjects": "Matematik B, Matematik C, Matematik Folkeskole, ",
        "tutor_address": "Frederikssundsvej 54B, 2. th.",
        "tutor_amount_of_students": 0,
        "tutor_gym": "Frederiksberg Gymnasium",
        "tutor_qualification": "Not set by tutor",
        "tutor_uni": "Økonomi"
    },

As you can see, I only specified Excel but I got a tutor that can teach in Excel, and other programs. So I'm thinking that I need to see if it "contains" the specified program in the API call

CodePudding user response:

For the long term, you'll probably want to use a SQL database with a table for the tutors and a table with the different programs with a many-to-many relationship between them.

For now, we can create some helper functions. It's not strictly necessary, but it will make the code easier to read and maintain.

def matches_filter(filter_value, tutor_value):
    if isinstance(tutor_value, list):
        # We want to treat this as a set of values to match
        # instead of checking for equality
        # Note: if you have to do this often, consider using sets instead of lists to store these values.
        return set(filter_value).issubset(set(tutor_value))
    return filter_value == tutor_value

def matches_all_filters(filter_dict, tutor):
    return all(filter_key in tutor and matches_filter(filter_value, tutor[filter_key])
               for filter_key, filter_value in filter_dict.items())

def get_matching_tutors(filters):
    matching_tutors = [tutor for tutor in tutor_list
                       if matches_all_filters(filters, tutor]

    return jsonify(matching_tutors)
  •  Tags:  
  • Related