[英]Dict comprehension in Python not PEP8
My Sublime Linter for Python 2.7
reports that this line is not PEP8: 我的Sublime Linter for
Python 2.7
报告此行不是PEP8:
D = {k:get_distance(v) for k, v in D.items() if len(v) > 1}
in column 8
and 27
在第
8
和27
栏中
but when I insert a space after colon ( :
), I get a red exclamation mark in Sublime. 但是当我插入冒号后面输入一个空格(
:
),我得到了崇高的红色感叹号。
How to make this line PEP8
compliant? 如何使此线符合
PEP8
?
Update 更新资料
Sublime Text 2.0.2, build 2221 ; Sublime Text 2.0.2,内部版本2221; Sublime Linter v1.8 ;
Sublime Linter v1.8; Python PEP8 Lint v2013.10.11.06
Python PEP8 Lint v2013.10.11.06
Code works and produces output without error 代码工作并产生输出而没有错误
Insert a space after the :
; 在
:
之后插入一个空格。 this is still valid Python syntax: 这仍然是有效的Python语法:
D = {k: get_distance(v) for k, v in D.items() if len(v) > 1}
If you are getting a syntax error, you introduced that somewhere else. 如果遇到语法错误,请在其他地方进行介绍。 Perhaps you forgot to close a
)
parenthesis on the preceding line, for example. 例如,也许您忘了在前一行关闭(
)
括号。
Note that using a capital letter as a local variable is not PEP-8 compliant. 请注意,使用大写字母作为局部变量不符合PEP-8。 Pick better, lowercase_with_underscores names for local variables:
为局部变量选择更好的lowercase_with_underscores名称:
def solution(seq):
positions = defaultdict(list)
for i, item in enumerate(seq):
positions[item].append(i)
distances = {k: get_distance(v) for k, v in positions.items() if len(v) > 1}
return max(distances.value()) if distances else 0
Last but not least; 最后但并非最不重要的; your indices in the
positions
dictionary are ordered (incrementing), so get_distance()
should just return l[-1] - l[0]
to make it return a distance in O(1) constant time. 您的
positions
字典中的索引是有序的 (递增),因此get_distance()
应该只返回l[-1] - l[0]
以使其以O(1)恒定时间返回距离。
D = {k: get_distance(v) for k, v in D.items() if len(v) > 1}
This is not a syntactical error and is the correct PEP8 way to write it. 这不是语法错误,而是正确的PEP8编写方式。 Check that you do not have any hidden unicode character in there.
检查那里没有隐藏的unicode字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.