как отфильтровать словарь - PullRequest
0 голосов
/ 18 марта 2019

вывести словарь:

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

Мне нужно только u'person': [(95, 11, 474, 466)]

как это отфильтровать?

это часть словаря в моем коде:

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

пожалуйста, помогите мне

Заранее спасибо

1 Ответ

0 голосов
/ 18 марта 2019

Вы можете просто скопировать интересующие вас ключи в новый текст:

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 

Выход:

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

См. Понимание словаря Python

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...