【问题标题】:How to retrieve specific parts of dictionary in python如何在python中检索字典的特定部分
【发布时间】:2022-01-20 22:09:07
【问题描述】:

我只想从此字典中检索“left_eye”和“right_eye”,并在不同的 python 文件中使用它们。

'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])),
}

在我的其他 python 文件中,我可以使用 .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()

有没有一种方法可以让我只使用“left_eye”和“right_eye”而无需编辑字典?

【问题讨论】:

  • 你的意思是像i ['keypoints']['left_eye']?

标签: python dictionary


【解决方案1】:

operator.itemgetter 返回一个可调用对象,它将获取您指定的项目。

假设i是一个包含keypoints的映射

import operator

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

【讨论】:

    【解决方案2】:

    很简单:i['keypoints']['left_eye']。只是不要在 for 循环中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 2017-06-22
      • 2023-01-04
      相关资源
      最近更新 更多