簡體   English   中英

PySpark:對列表中的元素進行分組

[英]PySpark: Grouping elements in a list

所需的輸出-[((1,2),(3,4),5)]

rdd = sc.parallelize([1,2,3,4,5])
rdd.map(lambda x: ((x[0],x[1]),(x[2],x[3]),x[4])).collect()

但是,我得到了錯誤——

TypeError: 'int' object is not subscriptable

at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:456)
at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:592)
at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:575)
at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:410)
at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
at scala.collection.Iterator$class.foreach(Iterator.scala:891)

請更正代碼。 我在 Spark 中使用 Python

根據 Mohamed Ali JAMAOUI 的評論:

“如果你想每行有一個列表,在構造 rdd 時傳遞一個列表列表,就像這樣 rdd = sc.parallelize([[1,2,3,4,5]])”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM