• 技术文章 >常见问题 >Python常见问题

    Python字典如何换行

    月亮邮递员月亮邮递员2020-04-03 16:41:28原创11647

    Python字典如何换行

    Python字典换行的方法如下:

    1、换行时保证行尾是逗号即可

    a = {"key0": "val0",
         "key1": "val1",
         "key2": "val2"}

    2、在长度不影响阅读的情况下这种写法也是允许的:

    a = {"key0": "val0", "key1": "val1",
         "key2": "val2", "key3": "val3",
         "key4": "val4", "key5": "val5"}

    注意这种情况下,每一行第一个非空白字符都要和第一行的括号后的字符对齐,并且最后的括号是不换行(直接跟在最后一个元素后面)的。

    3、另外一种换行方法如下:

    a = {
        "key0": "val0",
        "key1": "val1",
        "key2": "val2"
    }
    a = {
        "key0": "val0",
        "key1": "val1",
        "key2": "val2"
        }

    或是

    a = {
        "key0": "val0", "key1": "val1",
        "key2": "val2", "key3": "val3",
        "key4": "val4", "key5": "val5"
    }
    a = {
        "key0": "val0", "key1": "val1",
        "key2": "val2", "key3": "val3",
        "key4": "val4", "key5": "val5"
        }

    这种情况下,内容的每一行都需要比第一行只多一层缩进(即4个空格)

    Python中文网,大量Python视频教程,欢迎学习!

    专题推荐:python 换行 字典
    上一篇:如何切换到Python2.7 下一篇:Python中怎么新建文件夹

    相关文章推荐

    • 什么叫Python运算符重载• 如何在终端打开Python文件• 如何切换到Python2.7

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网