python中嵌套字典的换行代码

Eri*_*uza 3 python dictionary line-breaks

使用Alexa 中的新实体解析,嵌套字典变得非常嵌套。引用深度嵌套值的最pythonic 方式是什么?我如何编写每行不超过 79 个字符的代码?

这是我目前拥有的,虽然它有效,但我很确定有更好的方法:

if 'VolumeQuantity' in intent['slots']:
    if 'resolutions' in intent['slots']['VolumeQuantity']:
        half_decibels = intent['slots']['VolumeQuantity']['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value']['name'].strip()
    elif 'value' in intent['slots']['VolumeQuantity']:
        half_decibels = intent['slots']['VolumeQuantity']['value'].strip()
Run Code Online (Sandbox Code Playgroud)

这是来自 alexa 的 json 的部分示例

  {
    "type": "IntentRequest",
    "requestId": "amzn1.echo-api.request.9a...11",
    "timestamp": "2018-03-28T20:37:21Z",
    "locale": "en-US",
    "intent": {
      "name": "RelativeVolumeIntent",
      "confirmationStatus": "NONE",
      "slots": {
        "VolumeQuantity": {
          "name": "VolumeQuantity",
          "confirmationStatus": "NONE"
        },
        "VolumeDirection": {
          "name": "VolumeDirection",
          "value": "softer",
          "resolutions": {
            "resolutionsPerAuthority": [
              {
                "authority": "amzn1.er-authority.echo-blah-blah-blah",
                "status": {
                  "code": "ER_SUCCESS_MATCH"
                },
                "values": [
                  {
                    "value": {
                      "name": "down",
                      "id": "down"
                    }
                  }
                ]
              }
            ]
          },
          "confirmationStatus": "NONE"
        }
      }
    },
    "dialogState": "STARTED"
  }
Run Code Online (Sandbox Code Playgroud)

tim*_*geb 6

您可能指的是嵌套字典,列表只接受整数索引。

无论如何,(ab?)在括号内使用隐含的续行,我认为这是非常可读的:

>>> d = {'a':{'b':{'c':'value'}}}
>>> (d
...     ['a']
...     ['b']
...     ['c']
... )
'value'
Run Code Online (Sandbox Code Playgroud)

或者

>>> (d['a']
...   ['b']
...   ['c'])
'value'
Run Code Online (Sandbox Code Playgroud)