-1

output the dictionary:

{u'person': [(95, 11, 474, 466)],
 u'chair': [(135, 410, 276, 587)], 
 u'book': [(127, 380, 161, 396)]}

I need only u'person': [(95, 11, 474, 466)]

how to filter this?

this is part of a dictionary in my code:

detected_objects = {}
# analyze all worthy detections
for x in range(worthy_detections):

    # capture the class of the detected object
    class_name = self._categories[int(classes[0][x])]

    # get the detection box around the object
    box_objects = boxes[0][x]

    # positions of the box are between 0 and 1, relative to the size of the image
    # we multiply them by the size of the image to get the box location in pixels
    ymin = int(box_objects[0] * height)
    xmin = int(box_objects[1] * width)
    ymax = int(box_objects[2] * height)
    xmax = int(box_objects[3] * width)

    if class_name not in detected_objects:
        detected_objects[class_name] = []


    detected_objects[class_name].append((ymin, xmin, ymax, xmax))
detected_objects = detected_objects
print detected_objects

please help me

Thank you in advance

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Redhwan
  • 927
  • 1
  • 9
  • 24
  • https://docs.python.org/3.7/tutorial/datastructures.html#dictionaries – bruno desthuilliers Mar 18 '19 at 09:54
  • I'm voting to close this question as off-topic because SO is not a substitute to reading the official documentation. – bruno desthuilliers Mar 18 '19 at 09:55
  • first of all, I am a beginner in python, then I didn't see the same my case in official documentation, thank you – Redhwan Mar 18 '19 at 11:33
  • If you're a beginner (no problem with being a beginner, we've all been - and still are - beginners), then the first thing you want to do - and that's part of what's expected _before_ you post a question here - is to read the doc and do the official tutorial (when there's one, but that's most often the case nowadays at least for languages and frameworks). If you had done the official Python tutorial, you would have found out how to get a specific key from a dict, and generalizing from there how to filter a dict based on specific key(s). – bruno desthuilliers Mar 18 '19 at 12:07
  • thank you so much for those recommendations – Redhwan Mar 19 '19 at 10:15

1 Answers1

1

You can simply copy the keys you are interested in over into a new dict:

detected_objects  = {u'person': [(95, 11, 474, 466)], 
                     u'chair': [(135, 410, 276, 587)], 
                     u'book': [(127, 380, 161, 396)]}

keys_to_keep = {u'person'}

# dictionary comprehension
filtered_results = { k:v for k,v in detected_objects.items() if k in keys_to_keep}
print  filtered_results 

Output:

{u'person': [(95, 11, 474, 466)]}

See Python Dictionary Comprehension

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69