Python“最佳格式化实践”用于列表、字典等

发布于 2024-09-28 14:01:11 字数 606 浏览 0 评论 0原文

我一直在查看 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',
             ...
             }

处理列表/字典的深度嵌套?

I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,

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

or

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

or

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

How do I handle deep nesting of lists/dictionaries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

不醒的梦 2024-10-05 14:01:11

根据PEP8风格指南,有两种格式化字典的方法:

mydict = {
    'key': 'value',
    'key': 'value',
    ...
    }

或者

mydict = {
    'key': 'value',
    'key': 'value',
    ...
}

如果你想要为了符合 PEP8,我想说任何其他内容在技术上都是错误的。

According to the PEP8 style guide there are two ways to format a dictionary:

mydict = {
    'key': 'value',
    'key': 'value',
    ...
    }

OR

mydict = {
    'key': 'value',
    'key': 'value',
    ...
}

If you want to conform to PEP8 I would say anything else is technically wrong.

如梦亦如幻 2024-10-05 14:01:11

我首选的方式是:

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

My preferred way is:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}
柳若烟 2024-10-05 14:01:11

aaronasterling 的缩进风格是我喜欢的。这个以及其他几种样式在另一个SO问题中进行了解释。尤其是 Lennart Regebro 的回答给出了很好的概述。

但这种风格是投票最多的一种:

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

aaronasterling's indentation style is what I prefer. This, and several other styles are explained in another SO Question. Especially Lennart Regebro's answer gave a nice overview.

But this style was the one most voted for:

my_dictionary = {
    1: 'something',
    2: 'some other thing',
}
﹏半生如梦愿梦如真 2024-10-05 14:01:11

以您想要的任何方式定义您的字典,然后尝试以下操作:

from pprint import pprint

pprint(yourDict)

# for a short dictionary it returns:

{'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}

# for a longer/nested:

{'a00': {'b00': 0,
         'b01': 1,
         'b02': 2,
         'b03': 3,
         'b04': 4,
         'b05': 5,
         'b06': 6,
         'b07': 7,
         'b08': 8,
         'b09': 9},
 'a01': 1,
 'a02': 2,
 'a03': 3,
 'a04': 4,
 'a05': 5,
 'a06': 6,
 'a07': 7,
 'a08': 8,
 'a09': 9,
 'a10': 10}

您喜欢输出吗?

Define your dictionary in any way you want and then try this:

from pprint import pprint

pprint(yourDict)

# for a short dictionary it returns:

{'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}

# for a longer/nested:

{'a00': {'b00': 0,
         'b01': 1,
         'b02': 2,
         'b03': 3,
         'b04': 4,
         'b05': 5,
         'b06': 6,
         'b07': 7,
         'b08': 8,
         'b09': 9},
 'a01': 1,
 'a02': 2,
 'a03': 3,
 'a04': 4,
 'a05': 5,
 'a06': 6,
 'a07': 7,
 'a08': 8,
 'a09': 9,
 'a10': 10}

Do you like the output?

情绪失控 2024-10-05 14:01:11

如果您使用 ganeti(尊重 PEP 8),您应该选择第三个选项。

something = {
             'foo1': 'bar1',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             }

我喜欢这个,特别是。因为您可以选择您想要的元素。我觉得这样在两端删除或添加元素会更快。

注意: 正如评论中所指出的,根据 PEP,“:”(E203) 之前不应有空格。

If you go by ganeti (which respects PEP 8) you should choose the third option.

something = {
             'foo1': 'bar1',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             }

I like this esp. because you can select only the elements you want. And I feel removing or adding elements to either ends is faster this way.

Note: As pointed out in the comment there should be no whitespace before ':' (E203) as per PEP.

和我恋爱吧 2024-10-05 14:01:11

绝对不是选项 1,Python 的优点之一是它的易读性。选项 1 严重降低了可读性。

在 2 和 3 中,我将回应 pyfunc 为两者陈述的相同原因。

然而,在我自己的代码中,我更喜欢选项 3,因为第一个元素有时会因位于声明行末尾而“丢失”,并且在快速浏览代码时,有时我不会立即看到它。我知道这有点傻,但大脑以神秘的方式运作......

Definitely NOT option 1, one of the strenghts of Python is its legibility. Option 1 severely diminishes that legibility.

Out of 2 and 3, I'll echo the same reasons pyfunc stated for both.

However, in my own code I prefer option 3 simply because the first element sometimes gets 'lost' by being at the end of the declare line, and upon quick glancing at code sometimes I just won't see it immediately. I know it's a little silly, but the mind works in mysterious ways ...

对风讲故事 2024-10-05 14:01:11

好吧,第一个是不行的,因为你的行应该只有 79 个字符宽。至于其他两个选项,我认为这是一个品味问题,但我个人更喜欢第二个选项。

Well, the first one is a no-go, since your lines should only 79 characters wide. With regards to the other two options, I suppose it's a matter of taste, but I personally prefer the second option.

丶视觉 2024-10-05 14:01:11

我更喜欢第二个或第三个。

原因:

  1. 每个元素都位于自己的行上
  2. 在文本编辑器中到达行尾添加新元素是一件痛苦的事情
  3. 添加新元素很容易
  4. 使用第三个选项,有时您可以通过选择这些行来检查元素的数量。大多数编辑器会告诉您所选行的数量。

I prefer the second or third one.

Reason:

  1. Each element is on its own line
  2. Reaching to end of line to add a new element is a pain in a text editor
  3. Adding a new element is easy
  4. With the third option, sometimes you can check the number of elements by selecting those lines. Most editors will tell you the number of selected lines.
GRAY°灰色天空 2024-10-05 14:01:11

在阅读这篇文章之前,我会选择您提供的第三个选项。但现在我可能会选择一种不是 Török Gábor 风格的:

my_dictionary = {
1:“某事”,
2:“其他一些事情”,

老实说,除了你的第一个选择之外的任何东西都可能没问题。

Previous to reading this post I would have opted for the third option you give. But now I might go for the one that is NOT Török Gábor's style:

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

But honestly anything aside from your first option is probably fine.

水溶 2024-10-05 14:01:11

我喜欢第二种方式:

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

I love the second way:

something = {'foo' : 'bar',
         'foo2' : 'bar2',
         'foo3' : 'bar3',
         ...
         'fooN': 'barN'}
脸赞 2024-10-05 14:01:11

我想提一下以下选项,该选项在 PEP8 中没有具体提及,但在 字典文档:“当键是简单字符串时,有时使用关键字参数指定对会更容易:”

my_dict = dict(
    foo = 1,
    bar = 2,
    baz = 3,
    ...
)

但是,它并没有解决缩进问题。

I want to mention the following option, which is not specifically mentioned in the PEP8, but is noted in the dictionary documentation: "When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:"

my_dict = dict(
    foo = 1,
    bar = 2,
    baz = 3,
    ...
)

It doesn't settle the indentation question, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文