Pyro4:如何在 Java 中使用 Pyrolite 转换元组列表?

Pyro4: how to cast a list of tuples with Pyrolite in Java?

安装 Pyro4 and adding pyrolite and serpent jars to Java build path, using codes similar to the client and server side python codes which work OK with the Python client codes, I use some codes similar to the Java example on https://pythonhosted.org/Pyro4/pyrolite.html 后访问 Python 服务器对象。

import net.razorvine.pyro.*;

NameServerProxy ns = NameServerProxy.locateNS("127.0.0.1");
PyroProxy remoteobject = new PyroProxy(ns.lookup("example.greeting"));
List<String> inputWords = getExampleWords();
Object result = remoteobject.call("predictedResults", inputWords );
System.out.println("result = "+result );
remoteobject.close();
ns.close();

打印的 python 客户端结果类似于:

>> print 'result = \n' + str(result)

result =
[(u'aaa', 0.88877496454662064), (u'bbb', 0.019192749769012061), (u'?', 0.0070963814247323219), (u'ccc', 0.0068011253515576015), (u'home', 0.00148328236618)]

数据结构是一个元组列表,每个元组是一对字符串和浮点数,如:

result = []
for i in xrange(10):
    result.append((list[i], value[i])) 

控制台上打印的Java结果是:

>> Object result = remoteobject.call("predictedResults", inputWords );
>> System.out.println("result = "+result );

result = [[Ljava.lang.Object;@2e0fa5d3, [Ljava.lang.Object;@5010be6, [Ljava.lang.Object;@685f4c2e, [Ljava.lang.Object;@7daf6ecc, [Ljava.lang.Object;@2e5d6d97]

问题是:如何将 'Object result = remoteobject.call("predictedResults", inputWords );' 成功转换为 Java 中的字符串和浮点对列表?

经过反复试验,我终于找到了答案:

Object result = remoteobject.call("predictedResults", inputWords );
System.out.println("result = " + result);

ArrayList<Object[]> resultList = (ArrayList<Object[]>) result;
for (Object element : resultList) {
    Object[] pair = (Object[]) element;
    System.out.println((String)pair[0] + ", " + (Double)pair[1]); 
}