在Python中格式化多行dict的正确方法是什么?
在Python中,我想在代码中编写一个多行dict。有几种方法可以格式化它。以下是我能想到的几个: 我知道上述任何一种语法都是正确的,但我假设Python dict有一种首选的缩进和换行样式。这是什么在Python中格式化多行dict的正确方法是什么?,python,indentation,code-formatting,multiline,Python,Indentation,Code Formatting,Multiline,在Python中,我想在代码中编写一个多行dict。有几种方法可以格式化它。以下是我能想到的几个: 我知道上述任何一种语法都是正确的,但我假设Python dict有一种首选的缩进和换行样式。这是什么 注意:这不是语法问题。以上所有内容(据我所知)都是有效的Python语句,并且彼此等效。根据我在教程和其他方面的经验,第2项似乎总是首选,但这是个人的首选,而不是其他任何东西。我使用#3。长列表、元组等也一样。它不需要在缩进之外添加任何额外的空格。一如既往,保持一致 mydict = {
注意:这不是语法问题。以上所有内容(据我所知)都是有效的Python语句,并且彼此等效。根据我在教程和其他方面的经验,第2项似乎总是首选,但这是个人的首选,而不是其他任何东西。我使用#3。长列表、元组等也一样。它不需要在缩进之外添加任何额外的空格。一如既往,保持一致
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
(1, 'hello'),
(2, 'world'),
]
nested = {
a: [
(1, 'a'),
(2, 'b'),
],
b: [
(3, 'c'),
(4, 'd'),
],
}
类似地,这里是我在不引入任何空格的情况下包含大字符串的首选方法(如使用三引号多行字符串):
由于您的键是字符串,而且我们讨论的是可读性,因此我更喜欢:
mydict = dict(
key1 = 1,
key2 = 2,
key3 = 3
)
一般来说,您不会在最后一个条目后包含逗号,但Python会为您更正。首先,正如Steven Rumbalski所说,“PEP8不会解决这个问题”,因此这是个人偏好的问题 我将使用与您的格式3相似但不完全相同的格式。这是我的,为什么
my_dictionary = { # Don't think dict(...) notation has more readability
"key1": 1, # Indent by one press of TAB (i.e. 4 spaces)
"key2": 2, # Same indentation scale as above
"key3": 3, # Keep this final comma, so that future addition won't show up as 2-lines change in code diff
} # My favorite: SAME indentation AS ABOVE, to emphasize this bracket is still part of the above code block!
the_next_line_of_code() # Otherwise the previous line would look like the begin of this part of code
bad_example = {
"foo": "bar", # Don't do this. Unnecessary indentation wastes screen space
"hello": "world" # Don't do this. Omitting the comma is not good.
} # You see? This line visually "joins" the next line when in a glance
the_next_line_of_code()
btw_this_is_a_function_with_long_name_or_with_lots_of_parameters(
foo='hello world', # So I put one parameter per line
bar=123, # And yeah, this extra comma here is harmless too;
# I bet not many people knew/tried this.
# Oh did I just show you how to write
# multiple-line inline comment here?
# Basically, same indentation forms a natural paragraph.
) # Indentation here. Same idea as the long dict case.
the_next_line_of_code()
# By the way, now you see how I prefer inline comment to document the very line.
# I think this inline style is more compact.
# Otherwise you will need extra blank line to split the comment and its code from others.
some_normal_code()
# hi this function is blah blah
some_code_need_extra_explanation()
some_normal_code()
通常,如果您有大型python对象,则很难格式化它们。我个人更喜欢使用一些工具
这是一个可以立即将您的数据转换为可自定义样式的方法。对于1和2:括号内没有空格,请参见PEP 8。我想说的是,在pythons pprint模块中,它使用了您的第一个示例,括号内没有空格。您是否可以提供一些参考,我很难找到关于这方面的权威来源。(我同意你的看法)。不要告诉他,但那个用户不知道他在说什么;更严重的是,我也找不到“权威”的参考资料。如果我这样做了,我会让你知道的!也许有人应该联系Guido。这符合PEP 8:。缩进部分底部有一些列表示例。您的“嵌套”示例语法无效。否!始终包含最后一个逗号,因此如果添加新的最后一个元素,则不必更改它前面的行。这是Python的一大优点:实用性胜过纯粹性。此外,这个答案并没有解决问题。不是每个人都喜欢尾随逗号,也许我们中的少数OCD患者更喜欢阅读更干净的代码。我喜欢在线评论。我的第一位编程教授(我已经编程多年了)坚持使用内联注释,但从未有效地解释过原因。你现在已经解释了一个我已经用了大约20年的方法。啊哈,谢谢。在编程方面,我们有相似的年龄、经验和“里程数”。那么,如果你已经在20年前开始了在线评论的实践(这真是令人印象深刻!),那么你为什么在大概10年前你还在大学的时候还需要教授的解释呢?只是好奇而已。:-)非常好的问题:)ATARI BASIC和GWbasic实际上是强制使用它的,它们是自顶向下的基于流水线的编译器。这是我在纸质杂志上阅读peter norton的基本(以及后来的ASM代码)时采用的。其间我学习了Turbo Pascal,但我已经学习了纸质杂志中的示例,并遵守了BASIC的限制。PEP8在某种程度上解决了这一问题,因为它建议不要在开口大括号后立即添加空格,因此OP中的选项1和2不适用。在定义KWARG时更喜欢不使用空格
c=function(a=1,b=2)
更“pythonic”。这并不能回答问题
dict(rank = int(lst[0]),
grade = str(lst[1]),
channel=str(lst[2])),
videos = float(lst[3].replace(",", " ")),
subscribers = float(lst[4].replace(",", "")),
views = float(lst[5].replace(",", "")))
data = (
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh1"
"AAAABJRU5ErkJggg=="
)
mydict = dict(
key1 = 1,
key2 = 2,
key3 = 3
)
my_dictionary = { # Don't think dict(...) notation has more readability
"key1": 1, # Indent by one press of TAB (i.e. 4 spaces)
"key2": 2, # Same indentation scale as above
"key3": 3, # Keep this final comma, so that future addition won't show up as 2-lines change in code diff
} # My favorite: SAME indentation AS ABOVE, to emphasize this bracket is still part of the above code block!
the_next_line_of_code() # Otherwise the previous line would look like the begin of this part of code
bad_example = {
"foo": "bar", # Don't do this. Unnecessary indentation wastes screen space
"hello": "world" # Don't do this. Omitting the comma is not good.
} # You see? This line visually "joins" the next line when in a glance
the_next_line_of_code()
btw_this_is_a_function_with_long_name_or_with_lots_of_parameters(
foo='hello world', # So I put one parameter per line
bar=123, # And yeah, this extra comma here is harmless too;
# I bet not many people knew/tried this.
# Oh did I just show you how to write
# multiple-line inline comment here?
# Basically, same indentation forms a natural paragraph.
) # Indentation here. Same idea as the long dict case.
the_next_line_of_code()
# By the way, now you see how I prefer inline comment to document the very line.
# I think this inline style is more compact.
# Otherwise you will need extra blank line to split the comment and its code from others.
some_normal_code()
# hi this function is blah blah
some_code_need_extra_explanation()
some_normal_code()
dict(rank = int(lst[0]),
grade = str(lst[1]),
channel=str(lst[2])),
videos = float(lst[3].replace(",", " ")),
subscribers = float(lst[4].replace(",", "")),
views = float(lst[5].replace(",", "")))