问题:python中列表推导或生成器表达式的行连续

您应该如何分解很长的清单理解力?

[something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long]

我还看到某个地方的人不喜欢使用’\’来分隔行,但从不理解为什么。这是什么原因呢?

How are you supposed to break up a very long list comprehension?

[something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long]

I have also seen somewhere that people that dislike using ‘\’ to break up lines, but never understood why. What is the reason behind this?


回答 0

[x
 for
 x
 in
 (1,2,3)
]

效果很好,因此您几乎可以随心所欲。我个人更喜欢

 [something_that_is_pretty_long
  for something_that_is_pretty_long
  in somethings_that_are_pretty_long]

\不能被很好理解的原因是它出现在一行的末尾,要么不突出,要么需要额外的填充,当行长改变时必须固定它:

x = very_long_term                     \
  + even_longer_term_than_the_previous \
  + a_third_term

在这种情况下,请使用括号:

x = (very_long_term
     + even_longer_term_than_the_previous
     + a_third_term)
[x
 for
 x
 in
 (1,2,3)
]

works fine, so you can pretty much do as you please. I’d personally prefer

 [something_that_is_pretty_long
  for something_that_is_pretty_long
  in somethings_that_are_pretty_long]

The reason why \ isn’t appreciated very much is that it appears at the end of a line, where it either doesn’t stand out or needs extra padding, which has to be fixed when line lengths change:

x = very_long_term                     \
  + even_longer_term_than_the_previous \
  + a_third_term

In such cases, use parens:

x = (very_long_term
     + even_longer_term_than_the_previous
     + a_third_term)

回答 1

我不反对:

variable = [something_that_is_pretty_long
            for something_that_is_pretty_long
            in somethings_that_are_pretty_long]

\在这种情况下,您不需要。总的来说,我认为人们避免\因为它有点丑陋,但是如果不是最后一件事,它也会给您带来麻烦(请确保没有空格后跟)。我认为使用它比不使用它要好得多,这样可以减少行长。

由于\在上述情况下或对于带括号的表达式不是必需的,所以我实际上很少需要使用它。

I’m not opposed to:

variable = [something_that_is_pretty_long
            for something_that_is_pretty_long
            in somethings_that_are_pretty_long]

You don’t need \ in this case. In general, I think people avoid \ because it’s slightly ugly, but also can give problems if it’s not the very last thing on the line (make sure no whitespace follows it). I think it’s much better to use it than not, though, in order to keep your line lengths down.

Since \ isn’t necessary in the above case, or for parenthesized expressions, I actually find it fairly rare that I even need to use it.


回答 2

在处理多个数据结构的列表时,也可以使用多个缩进。

new_list = [
    {
        'attribute 1': a_very_long_item.attribute1,
        'attribute 2': a_very_long_item.attribute2,
        'list_attribute': [
            {
                'dict_key_1': attribute_item.attribute2,
                'dict_key_2': attribute_item.attribute2
            }
            for attribute_item
            in a_very_long_item.list_of_items
         ]
    }
    for a_very_long_item
    in a_very_long_list
    if a_very_long_item not in [some_other_long_item
        for some_other_long_item 
        in some_other_long_list
    ]
]

请注意,它还如何使用if语句过滤到另一个列表。将if语句放到自己的行中也是有用的。

You can also make use of multiple indentations in cases where you’re dealing with a list of several data structures.

new_list = [
    {
        'attribute 1': a_very_long_item.attribute1,
        'attribute 2': a_very_long_item.attribute2,
        'list_attribute': [
            {
                'dict_key_1': attribute_item.attribute2,
                'dict_key_2': attribute_item.attribute2
            }
            for attribute_item
            in a_very_long_item.list_of_items
         ]
    }
    for a_very_long_item
    in a_very_long_list
    if a_very_long_item not in [some_other_long_item
        for some_other_long_item 
        in some_other_long_list
    ]
]

Notice how it also filters onto another list using an if statement. Dropping the if statement to its own line is useful as well.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。