问题:在Python中格式化多行字典的正确方法是什么?
在Python中,我想在代码中编写多行字典。有几种方法可以格式化它。我想到的是一些:
mydict = { "key1": 1, "key2": 2, "key3": 3, }
mydict = { "key1": 1, "key2": 2, "key3": 3, }
mydict = { "key1": 1, "key2": 2, "key3": 3, }
我知道以上任何一种在语法上都是正确的,但是我假设Python字典有一种首选的缩进和换行样式。它是什么?
注意:这不是语法问题。就我所知,以上所有都是有效的Python语句,并且彼此等效。
回答 0
我使用#3。长列表,元组等也是如此。不需要在缩进之外添加任何额外的空格。一如既往,保持一致。
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
(1, 'hello'),
(2, 'world'),
]
nested = {
a: [
(1, 'a'),
(2, 'b'),
],
b: [
(3, 'c'),
(4, 'd'),
],
}
同样,这是在不引入任何空格的情况下包括大字符串的我的首选方式(例如,如果使用三引号的多行字符串,则会得到此信息):
data = (
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh1"
"AAAABJRU5ErkJggg=="
)
回答 1
首先,就像史蒂文·鲁姆巴尔斯基(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()
回答 2
由于您的键是字符串,并且因为我们在谈论可读性,所以我更喜欢:
mydict = dict(
key1 = 1,
key2 = 2,
key3 = 3,
)
回答 3
通常,如果您有大型python对象,则很难格式化它们。我个人更喜欢为此使用一些工具。
这是python-beautifier-www.cleancss.com/python-beautify,可立即将您的数据转换为可自定义的样式。
回答 4
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(",", "")))
回答 5
根据我在教程和其他方面的经验,似乎总是首选2号,但这是个人喜好选择,而不是其他任何事情。
回答 6
通常,您不会在最后一个输入项后加入逗号,但Python会为您更正。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。