Jython塔架映射到列表<;字符串>;以及其他Jython到Java变量的映射
如何迭代Jython PyList并将包含的对象转换为java.lang.String 在以前的教程中,它是通过使用_uu_u; tojava __;来完成的,比如:Jython塔架映射到列表<;字符串>;以及其他Jython到Java变量的映射,java,python,list,jython,Java,Python,List,Jython,如何迭代Jython PyList并将包含的对象转换为java.lang.String 在以前的教程中,它是通过使用_uu_u; tojava __;来完成的,比如: (EmployeeType)employeeObj.__tojava__(EmployeeType.class); 我支持这可能是这样的: PyList pywords = pythonFactoryCreatedObject.pythonMethodReturningPyList() int count = pyword
(EmployeeType)employeeObj.__tojava__(EmployeeType.class);
我支持这可能是这样的:
PyList pywords = pythonFactoryCreatedObject.pythonMethodReturningPyList()
int count = pywords.__len__();
for (int idx = 0 ; idx < count ; idx++) {
PyObject obj = pywords.__getitem__(idx);
//here i do not know how to have a kind of 'String word = pywords[idx]' statement
//System.out.println(word);
}
PyList pywords=pythonFactoryCreatedObject.pythonMethodReturningPyList()
int count=pywords.uu len_uuu();
for(int idx=0;idx- 从PyList到java数组或列表的转换?这样就可以使用“for(String word:mylist){}”构造了吗
- 简单的python字典映射到适当的java对象也会遇到同样的问题,最好的映射是什么
best
PyList
实际上实现了java.util.List
,所以您可以直接从java端使用它。
如果用字符串填充,则其元素将是PyString
(或者可能是PyUnicode
)。
因此:
或
不管你觉得哪个更清楚
编辑:
. 从Java使用Jython的更好方法是从Jython实现Java接口并从Java操作接口,但您似乎正在使用现有的Python代码库,因此如果没有一些更改,这将无法工作。感谢您的有用解释!我没有理解使用“toJava”的要点,你的回答帮助我更好地理解。
List pywords = pythonFactoryCreatedObject.pythonMethodReturningPyList();
for (Object o : pyList){
String string = ((PyString) o).getString();
//whatever you want to do with it
}
List pywords = pythonFactoryCreatedObject.pythonMethodReturningPyList()
for (Object o : pyList){
String string = ((PyObject) o).__toJava__(String.class);
//whatever you want to do with it
}