【问题标题】:Python dictionary function resulting in syntax error on Linux but not on Windows [duplicate]Python字典函数在Linux上导致语法错误,但在Windows上没有[重复]
【发布时间】:2015-09-10 21:01:52
【问题描述】:

我有一行代码通过将其键设置为dictionary_b 的键来创建字典。然后以dictionary_b中那个key的值作为key,通过dictionary_a来查找对应的值。

new_dictionary = {key : dictionary_a.get(value, value) for key, value in dictionary_b.iteritems()}

这在 Windows 上编译得很好,但是当我尝试在 Linux 上使用 compile 时出现以下错误:

  File "IP_Merger.py", line 46
    new_ip = {key : dictionary_a.get(value, value) for key, value in dictionary_b.iteritems()}
                                                     ^
SyntaxError: invalid syntax

在 Windows 上,我在 Python 2.7 和 Linux 2.6.6 上进行编译。会不会是这个问题?

【问题讨论】:

  • 是的,这就是问题

标签: python dictionary syntax


【解决方案1】:

从 Python 2.7 开始支持字典推导 - 请参阅 this quesiton 了解更多信息。

您可以在 python 2.6 中使用生成器创建字典,如下所示:

dict((key(s),value(s)) for s in something)

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多