如何从lis中获取一些值

2024-09-30 06:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个转换dict,我从API得到,我应该从routes得到一个名称。当我试图得到它时,我总是得到第一个值

我在一个例子中尝试了它(我没有搜索它的例子,我在我的代码上显示它Namereponsibleroutes = alltext[i]['routes'][0]['name']),但是我有第一个值,没有它[0],我有一个错误TypeError: list indices must be integers or slices, not str

alltext=[{'category': {'id': 'd41d97eb-6ecc-4aed-b9be-4fca3411d2c8',
               'name': 'С',
               'vendorId': 'С'},
  'format': {'id': 'fad3d784-6a79-4af8-bf66-db214fadef11',
             'name': 'магазин',
             'vendorId': '1'},
  'freeVisit': False,
  'id': 'fd65865b-0f43-468b-80a5-04d5c9f90086',
  'name': 'магазин-кафетерій',
  'relationships': [{'orderIndex': 1,
                     'relationship': {'deferment': 90,
                                      'id': '44a3297a-37fc-4844-a6ee-43de0a8bfcf3',
                                      'limit': 0.0,
                                      'name': 'Договор № 32207330',
                                      'vendorId': '32207330'}}],
  'routes': [{'dateFrom': '2018-01-01T00:00:00Z',
              'id': '9466dd46-53c8-4ff4-bfaa-0115262eeed9',
              'name': 'string1',
              'vendorId': '154788512'},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'id': 'eac2a7e1-b9c7-4b14-b165-21f5e95bd71f',
              'name': 'string2',
              'orderPricePolicy': {'discountPercentEnabled': False,
                                   'discountPercentFrom': 0.0,
                                   'discountPercentIsFractional': False,
                                   'discountPercentTill': 0.0,
                                   'markupPercentEnabled': False,
                                   'markupPercentFrom': 0.0,
                                   'markupPercentIsFractional': False,
                                   'markupPercentTill': 0.0,
                                   'priceEditEnabled': True}},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'fetchMethod': 'byRelationships',
              'id': 'a2890f23-a1d9-458c-9755-3deed8fc3153',
              'name': 'string3',
              'orderPricePolicy': {'discountPercentEnabled': True,
                                   'discountPercentFrom': 1.1,
                                   'discountPercentIsFractional': True,
                                   'discountPercentTill': 5.5,
                                   'markupPercentEnabled': False,
                                   'markupPercentFrom': 0.0,
                                   'markupPercentIsFractional': False,
                                   'markupPercentTill': 0.0,
                                   'priceEditEnabled': True},
              'refundPricePolicy': {'discountPercentEnabled': False,
                                    'discountPercentFrom': 0.0,
                                    'discountPercentIsFractional': False,
                                    'discountPercentTill': 0.0,
                                    'markupPercentEnabled': True,
                                    'markupPercentFrom': 5.5,
                                    'markupPercentIsFractional': True,
                                    'markupPercentTill': 7.7,
                                    'priceEditEnabled': False}},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'id': '174ead71-d11a-427f-9a15-70dd3606b51e',
              'name': 'string4'},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'fetchMethod': 'byRelationships',
              'id': 'b89975cc-5043-4bb9-9aa5-a2330b217d74',
              'name': '666',
              'vendorId': '666'},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'id': '6bc4381a-b7a1-4c35-98a2-ab58400c8689',
              'name': 'string5'},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'id': 'ee16712c-f63c-492d-a4f5-cc6158f77a4e',
              'name': 'string6',
              'orderPricePolicy': {'discountPercentEnabled': True,
                                   'discountPercentFrom': 10.0,
                                   'discountPercentIsFractional': False,
                                   'discountPercentTill': 30.0,
                                   'markupPercentEnabled': True,
                                   'markupPercentFrom': 40.0,
                                   'markupPercentIsFractional': False,
                                   'markupPercentTill': 70.0,
                                   'priceEditEnabled': True},
              'vendorId': '1050'},
             {'dateFrom': '2018-01-01T00:00:00Z',
              'id': 'bf57c2b6-035f-4c21-b8c2-e102eed032d1',
              'name': 'string7'}],
  'vendorId': '232231'}]


        for i in range(len(alltext)):
            Namereponsibleroutes = alltext[i]['routes']['name']

            print(Namereponsibleroutes)

我期望输出string1、string2、string3、string4、string5、string6、string7,但实际输出是string1


Tags: nameidfalsetrueroutesalltextvendoriddatefrom
1条回答
网友
1楼 · 发布于 2024-09-30 06:13:49

alltext[i]['routes']是词典列表。因此,要从列表中的每个字典中获取键'name'的值,必须提取每个字典。你可以用列表来做这件事

Nameresponsibleroutes = [routeDict['name'] for routeDict in alltext[i]['routes']]
# I am assuming that alltext usually has more than one dictionary in it, otherwise if it is always only 1, change `i` to `0` or don't have it be a list

相关问题 更多 >

    热门问题