我想把这两行代码合二为一。
for n in exceptions:
my_dict[n] += 1
令我困扰的是,一个用于递增字典的小语句需要两行。我相信这个问题也困扰着某些人。
请您参考如下方法:
for n in exceptions: my_dict[n] += 1
...但是没有任何问题,因为该语句需要两行。有一些python coding guidelines (PEP8)他们强烈鼓励核心可读性。将这些语句放在一行中会降低可读性。
来自 PEP8 :
While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!
