Python List of tuples in Scala

Ark

I am using Jython execute the python code part (a python module with utility functions from existing codebase) that returns a list of tuples, but what I get in scala is a simple flattened list. Any suggestions on the cause would help. Since I am a beginner with with Scala and Jython, this probably might not be the best approach to solve the problem. I call the python function as shown below:

val viaJython = true
val interp = new PythonInterpreter()
val pyCode =
  interp.compile(
    """import myModule as df
       | aList = df.find_date(foundVal)"""
  )

interp.set("foundVal", foundVal)
interp.exec(pyCode)
println(interp.get("aList"))
user4322779

A simple solution may be: If you get a flattened List and you know the tuple size n then it can be unflattened into a List of Lists with List.grouped(n).toList. Then the sublists can be converted to tuples with map as shown below or using methods given at Convert a Scala list to a tuple? and Is there way to create tuple from list(without codegeneration)?. If you do not know the tuple length then you can find out by examining the bytecode generated by javap -c on the class file.

Another method is to use the implicit conversions from Java collections, iterators, iterables and enumerators which Scala provides. To apply them add "import scala.collection.JavaConversions._" before executing Jython and set resulting Jython List to Scala mutable.Buffer with explicit type declaration. For example, if the Jython List elements are 3-tuples of Int (Tuple3[Int,Int,Int] in Scala) then the converted Scala collection could be defined as:

val pyBuffer: scala.collection.mutable.Buffer[Tuple3[Int,Int,Int]] = ResultingJavaListFromJython

The reason for using scala.collection.mutable.Buffer is that is the collection supported by scala.collection.JavaConversions for Java List conversion. After the conversion to mutable.Buffer is done, it can be converted to any of a number of other types of collections with its "to" functions including toList to convert it to a Scala List, e.g.:

val pyList = pyBuffer.toList

For reference see http://www.scala-lang.org/api/2.11.7/#scala.collection.JavaConversions$ or the equivilant API page for whatever version of Scala you are using.

Another issue is that Java does not have tuples so Jython implements PyTuple with java.util.List and Scala does not provide conversion to Tuple. For that reason another thing to try, assuming that each PyTuple has Int elements, is:

import scala.collection.mutable.Buffer
val pyBuffer2: Buffer[Buffer[Int]] = ResultingJavaListFromJython

Then the elements of pyBuffer2 can be mapped to tuples. For example, assuming each Buffer[Int] element has 3 elements:

import scala.collection.mutable.ArrayBuffer
val pyBuffer3 = pyBuffer2.map{case ArrayBuffer(a,b,c) => (a,b,c)}

Now pyBuffer3 can be converted to Scala List with toList as shown above.

The reason for importing scala.collection.mutable.ArrayBuffer and matching on it is that it is Scala's default implementation of mutable.ArrayBuffer which is a trait.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related