【发布时间】:2013-12-21 19:33:12
【问题描述】:
请有人帮我将一些 python 2.7 语法翻译成 python 2.6(由于 redhat 依赖关系,有点卡在 2.6 上)
所以我有一个简单的函数来构造一棵树:
def tree(): return defaultdict(tree)
当然我想以某种方式显示树。在 python 2.7 下我可以使用:
$ /usr/bin/python2.7
Python 2.7.2 (default, Oct 17 2012, 03:00:49)
[GCC 4.4.6 [TWW]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def dicts(t): return {k: dicts(t[k]) for k in t}
...
>>>
一切都好...但是在 2.6 下我收到以下错误:
$ /usr/bin/python
Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def dicts(t): return {k: dicts(t[k]) for k in t}
File "<stdin>", line 1
def dicts(t): return {k: dicts(t[k]) for k in t}
^
SyntaxError: invalid syntax
如何重写代码:
def dicts(t): return {k: dicts(t[k]) for k in t}
以便我可以在 python 2.6 下使用它?
【问题讨论】:
标签: python python-2.7 syntax python-2.6