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
有人能帮我解决这个问题吗
您使用的是python2.x风格的代码,但是解释器是3.x
你应该重构你的代码,使之与3.x兼容。我不建议您降级Python版本as Python 2.7 is reaching end of life/support soon
如果(出于某种原因)希望代码同时在Python2.x和3.x上运行,请考虑在文件开头添加以下行:
然后将您的
print
语句更改为print
调用:请注意,上面的解决方案专门针对您的
SyntaxError
,您可能仍然会遇到运行时故障,请参阅porting guide以获取更多帮助相关问题 更多 >
编程相关推荐