相关疑难解决方法(0)

Python括号约定

您认为在代码中编写字典文字时主要使用的约定是什么?

我会写一个可能的约定作为答案.

python coding-style conventions

5
推荐指数
3
解决办法
3043
查看次数

python中嵌套字典的换行代码

使用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", …
Run Code Online (Sandbox Code Playgroud)

python dictionary line-breaks

3
推荐指数
1
解决办法
1091
查看次数