python代码中的哪一部分产生语法错误?

2024-09-29 02:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

text=sc.textFile("long")
RDD20172630=text.flatMap(lambda line : line.split()).map(lambda word : (word,1)).reduceByKey(lambda c1, c2 : c1 + c2)
print RDD20172630.sortByKey(ascending=True).collect()
print RDD20172630.map(lambda (w,c): (c,w)).sortByKey(ascending=False).collect()

此代码的结果显示为错误,如下所示:

File "<ipython-input-1-14309b0de61d>", line 3
    print RDD20172630.sortByKey(ascending=True).collect()
                    ^
SyntaxError: invalid syntax

有人能帮我解决这个问题吗


Tags: lambdatexttruemaplinewordcollectsc
1条回答
网友
1楼 · 发布于 2024-09-29 02:16:11

您使用的是python2.x风格的代码,但是解释器是3.x

你应该重构你的代码,使之与3.x兼容。我不建议您降级Python版本as Python 2.7 is reaching end of life/support soon


如果(出于某种原因)希望代码同时在Python2.x和3.x上运行,请考虑在文件开头添加以下行:

from __future__ import print_function

然后将您的print语句更改为print调用:

print(RDD20172630.sortByKey(ascending=True).collect())
print(RDD20172630.map(lambda (w,c): (c,w)).sortByKey(ascending=False).collect())

请注意,上面的解决方案专门针对您的SyntaxError,您可能仍然会遇到运行时故障,请参阅porting guide以获取更多帮助

相关问题 更多 >