Python2.6 语法错误

Python2.6 syntax error

我正在 Python2.6 中编写一些脚本。我一直写在python2.7所以不知道为什么这句话是错的:

keys = ['h','b']
d = {k:0 for k in keys if not k in ['time_us', 'status']}

错误:

print (sys.version)

2.6.6 (r266:84292, Mar 15 2018, 13:11:05) [GCC 5.4.0 20160609]

keys = ['b','h']

d = {k:0 for k in keys if not k in ['time_us', 'status']}

File "", line 1

d = {k:0 for k in keys if not k in ['time_us', 'status']}
           ^ SyntaxError: invalid syntax

词典理解出现在 Python 2.7.

对于 Python 2.6,您可以传递 dict 一个 key-value 对序列。

d = dict((k,0) for k in keys if k not in ('time_us', 'status'))