Python列表、字典和,

2024-09-29 01:27:49 发布

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

我一直在查阅Python文档,以了解大型列表和字典的代码格式最佳实践,例如

something = {'foo' : 'bar', 'foo2' : 'bar2', 'foo3' : 'bar3'..... 200 chars wide, etc..}

或者

something = {'foo' : 'bar',
             'foo2' : 'bar2',
             'foo3' : 'bar3',
             ...
             }

或者

something = {
             'foo' : 'bar',
             'foo2' : 'bar2',
             'foo3' : 'bar3',
             ...
             }

如何处理列表/词典的深度嵌套?


Tags: 代码文档列表字典foo格式etcbar
2条回答

我更喜欢的方式是:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}

我更喜欢aaronasterling的压痕样式。在another SO Question中解释了这个和其他几种样式。尤其是雷吉布罗的回答给出了一个很好的概述。

但这种风格是最受欢迎的:

my_dictionary = {
    1: 'something',
    2: 'some other thing',
}

相关问题 更多 >