从 Python 列表中提取多个值

问题描述 投票:0回答:1

我有一本字典,已将其转换为列表,我的任务是根据标签创建产品列表。但是我不断收到一个空列表:

这是我的列表(values_list):

[['lipstick', 'spray', 'polish', 'other'],
 [{'annotated_regions': [{'tags': ['lipstick'],
     'region_type': 'Box',
     'region': {'xmin': 0.6413427734375,
      'ymin': 0.2342342380079588015,
      'xmax': 0.234234230703125,
      'ymax': 0.4645647705992509}},
    {'tags': ['polish'],
     'region_type': 'Box',
     'region': {'xmin': 0.234234334375,
      'ymin': 0.234234224531835,
      'xmax': 0.4564560546875,
      'ymax': 0.2342341481741573}},
    {'tags': ['lipstick'],
     'region_type': 'Box'

我的for循环:

lipstick_tag = []
for idx, item in enumerate(values_list):
    if 'tags' == 'lipstick':
        lipstick_tag.append(item)
python list dictionary
1个回答
0
投票

您的 for 循环期望数据的格式与您的

values_list
非常不同。试试这个吧

[item for item in values_list[1][0]['annotated_regions'] if 'lipstick' in item['tags']]

© www.soinside.com 2019 - 2024. All rights reserved.