简体   繁体   English

Scala中元组的Python列表

[英]Python List of tuples in Scala

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.我正在使用 Jython 执行返回元组列表的 python 代码部分(一个带有来自现有代码库的实用程序函数的 python 模块),但我在 scala 中得到的是一个简单的扁平列表。 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.由于我是 Scala 和 Jython 的初学者,这可能不是解决问题的最佳方法。 I call the python function as shown below:我调用python函数如下所示:

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"))

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.一个简单的解决方案可能是:如果你得到一个扁平化的 List 并且你知道元组大小 n 那么它可以用 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?然后可以将子列表转换为具有映射的元组,如下所示或使用将 Scala 列表转换为元组中给出的方法 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.如果您不知道元组长度,则可以通过检查 javap -c 在类文件上生成的字节码来找出。

Another method is to use the implicit conversions from Java collections, iterators, iterables and enumerators which Scala provides.另一种方法是使用 Scala 提供的 Java 集合、迭代器、迭代器和枚举器的隐式转换。 To apply them add "import scala.collection.JavaConversions._" before executing Jython and set resulting Jython List to Scala mutable.Buffer with explicit type declaration.要应用它们,请在执行 Jython 之前添加“import scala.collection.JavaConversions._”,并使用显式类型声明将结果 Jython 列表设置为 Scala mutable.Buffer。 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:例如,如果 Jython 列表元素是 Int 的 3 元组(Scala 中的 Tuple3[Int,Int,Int]),那么转换后的 Scala 集合可以定义为:

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.使用 scala.collection.mutable.Buffer 的原因是 scala.collection.JavaConversions 支持的用于 Java List 转换的集合。 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, eg:转换为 mutable.Buffer 后,它可以转换为许多其他类型的集合中的任何一种,其“to”函数包括 toList 将其转换为 Scala 列表,例如:

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.如需参考,请参阅http://www.scala-lang.org/api/2.11.7/#scala.collection.JavaConversions $ 或您使用的任何版本的 Scala 的等效 API 页面。

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.另一个问题是 Java 没有元组,因此 Jython 使用 java.util.List 实现 PyTuple,而 Scala 不提供到元组的转换。 For that reason another thing to try, assuming that each PyTuple has Int elements, is:出于这个原因,假设每个 PyTuple 都有 Int 元素,要尝试的另一件事是:

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

Then the elements of pyBuffer2 can be mapped to tuples.然后可以将 pyBuffer2 的元素映射到元组。 For example, assuming each Buffer[Int] element has 3 elements:例如,假设每个 Buffer[Int] 元素有 3 个元素:

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.现在可以使用 toList 将 pyBuffer3 转换为 Scala List,如上所示。

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.导入 scala.collection.mutable.ArrayBuffer 并对其进行匹配的原因是它是 Scala 对 mutable.ArrayBuffer 的默认实现,这是一个 trait。

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

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