Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve specific parts of dictionary in python

I want to only retrieve ‘left_eye’ and ‘right_eye’ from this dictionary and use them in a different python file.

'keypoints': {
        'left_eye': (int(keypoints[0]), int(keypoints[5])),
        'right_eye': (int(keypoints[1]), int(keypoints[6])),
        'nose': (int(keypoints[2]), int(keypoints[7])),
        'mouth_left': (int(keypoints[3]), int(keypoints[8])),
        'mouth_right': (int(keypoints[4]), int(keypoints[9])),
}

In my other python file I am able to retrieve all of them by using .items()

def face_points():

    add = find_axis().add_patch
    
    for value, number in i ['keypoints'].items():

        add(shape.Circle(
        number, 
        color='white', 
        radius=1, 
        fill=True))

face_points()

Is there a way I can only use ‘left_eye’ and ‘right_eye’ without having to edit the dictionary?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

operator.itemgetter returns a callable that will get only the items you specify.

Assuming i is a mapping that contains keypoints

import operator

what_i_want = operator.itemgetter('left_eye','right_eye')
left,right = what_i_want(i['keypoints'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading