简体   繁体   English

SyntaxError:语法无效

[英]SyntaxError: invalid syntax

I am using Python 2.6.6 and is a newbie. 我正在使用Python 2.6.6,并且是新手。 I am getting 我正进入(状态

  File "./factorizer.py", line 35
    return {n: factorize_naive(n) for n in nums}
                                    ^
SyntaxError: invalid syntax

in the for statement within the return. 在for语句内返回。 It is supposed to return a dict with each element having number as key and list of prime factors as value. 它应该返回一个字典,每个元素都以数字为键,素数列表作为值。 Where am I going wrong? 我要去哪里错了?

Dictionary comprehensions are only available in Python 2.7 and above. 字典理解仅在Python 2.7及更高版本中可用。 You'll need to use another method, preferably using dict() : 您将需要使用另一种方法,最好使用dict()

dict((i, factorize_naive(i)) for i in nums)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM