java传递1个list参数
@Test public void oneList() { List<Double> list = new ArrayList<>(); list.add(1.222); list.add(2.888); list.add(3.888); list.add(4.888); list.add(5.888); String flag = oneList(list); System.out.println(flag); }
public String oneList(List<Double> list) { String result = null; try { // String python = "/usr/local/bin/python3.10"; // python String python = "python"; String file = "/app/hello_list01_arg.py"; Process process = Runtime.getRuntime().exec(python + " " + file + " " + list);// 执行py文件 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = in.readLine()) != null) { result = line; } in.close(); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return result; }
hello_list01_arg.py
import numpy as np import sys ''' 接受java传递1个list参数 ''' def main(): list_str = [] for i in range(1, len(sys.argv)): list_str.append(sys.argv[i].replace(",", "")) list_str[0] = list_str[0].replace("[", "") list_str[len(sys.argv) - 2] = list_str[len(sys.argv) - 2].replace("]", "") # 字符串数组 print(list_str) if __name__ == '__main__': main()
java传递2个list参数
@Test public void towList() { List<Double> listOne = new ArrayList<>(); listOne.add(1.222); listOne.add(2.888); listOne.add(3.888); listOne.add(4.888); listOne.add(5.888); List<Integer> listTwo = new ArrayList<>(); listTwo.add(1); listTwo.add(2); listTwo.add(3); listTwo.add(4); listTwo.add(5); String flag = towList(listOne, listTwo); System.out.println(flag); }
public String towList(List<Double> listOne, List<Integer> listTwo) { String result = null; try { // tring python = "/usr/local/bin/python3.10 "; // python String python = "python "; String file = ResourcesUtil.getResource("py", "hello_list02_arg.py"); result = ProcessUtils.exec(python + " " + file + " " + listOne + " " + listTwo); } catch (Exception e) { e.printStackTrace(); } return result; }
hello_list02_arg.py
import numpy as np import sys ''' 接受java传递2个list参数 ''' def main(): #初始数据 # print (sys.argv[1:]) list_str = [] turn = [] current = [] for i in range(1, len(sys.argv)): list_str.append(sys.argv[i].replace(",", "")) list_str[0] = list_str[0].replace("[", "") list_str[len(sys.argv) - 2] = list_str[len(sys.argv) - 2].replace("]", "") str = ','.join(list_str) arr = str.split("][") turn = arr[0].split(",") turn = list(map(float, turn))#转换成浮点数数组 #输出第一个数组 print (turn) current = arr[1].split(",") current = list(map(int, current))#转换成整数数组 #输出第二个数组 print (current) if __name__ == '__main__': main()