Java调用Python传list(多个list,不同类型list)参数

## 1、通过Runtime进行调用 #### 1.1 传一个字符串 * java文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class CoalType {
public static String coalType_list(String data) {
String arr = null;
try {
String[] arg = new String[] { "E:\\Program Files\\Python36\\python.exe", "E:\\Test\\test\\qiting.py",data};
Process proc = Runtime.getRuntime().exec(arg);// 执行py文件

BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
arr = line;
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arr;
}

public static void main(String[] args) {
String data = "Lisa";
String test_name = CoalType.coalType_list(data);
System.out.println(test_name);
}
}
  • py文件
1
2
3
4
5
6
7
8
import numpy as np
import sys as ss

def main():
print (ss.argv[1])

if __name__ == '__main__':
main()
  • 结果
1
Lisa

如下图:

   在Java中通过Runtime调用Python程序与直接执行Python程序的效果是一样的,可以在Python中读取传递的参数,也可以在Java中读取到Python的执行结果。
   如上图所示,这样是获取不到数据的,需要输出语句print,StartStopOpt方法在java中也调用不了,需要在.py文件里初始化调用,然后输出,java才能获取到数据
   需要注意的是,不能在Python中通过return语句返回结果,只能将返回值写入到标准输出流中,然后在Java中通过标准输入流读取Python的输出值。

1.1 传一个list集合

  • java文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    public class Mogun {
    public static String coalType_list(List<Double> list_one) {
    String arr = null;
    try {
    String sysPython = "E:\\Program Files\\Python36\\python.exe ";
    String filePython = "E:\\Test\\test\\qiting.py ";
    Process proc = Runtime.getRuntime().exec(sysPython + filePython + list_one);// 执行py文件
    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    arr = line;
    }
    in.close();
    proc.waitFor();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    return arr;
    }

    public static void main(String[] args) throws Exception {
    List<Double> list_one = new ArrayList<>();
    list_one.add(1.222);
    list_one.add(2.888);
    list_one.add(3.888);
    list_one.add(4.888);
    list_one.add(5.888);
    String flag = Mogun.coalType_list(list_one);
    System.out.println(flag);

    }
    }
  • py文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import numpy as np
    import sys as ss

    def StartStopOpt(list_str,list_float):
    ............
    ...........
    return result

    def main():
    list_str = []
    list_float = []
    for i in range(1, len(ss.argv)):
    list_str.append(ss.argv[i].replace(",", ""))
    list_str[0] = list_str[0].replace("[", "")
    list_str[len(ss.argv) - 2] = list_str[len(ss.argv) - 2].replace("]", "")
    # 字符串数组
    # print(list_str)
    # 浮点数数组
    list_int = list(map(float, list_str))
    # print(list_int)
    # 调用函数传参得到返回值输出
    test = StartStopOpt(list_str,list_float)
    print(test)

    if __name__ == '__main__':
    main()
  • 结果

    1
    2
    3
    4
    5
    6
    字符串数组
    ['1.222', '2.888', '3.888', '4.888', '5.888']
    浮点数数组
    [1.222, 2.888, 3.888, 4.888, 5.888]
    调用函数传参得到返回值输出
    ...省略

注意:一定要记得空格,不然会报错

1.1 传两个list集合

  • java文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    public class Mogun {
    public static String coalType_list(List<Double> list_one, List<Integer> list_two) {
    String arr = null;
    try {
    String sysPython = "E:\\Program Files\\Python36\\python.exe ";
    String filePython = "E:\\Test\\test\\qiting.py ";
    Process proc = Runtime.getRuntime().exec(sysPython + filePython + list_one + list_two);// 执行py文件
    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    arr = line;
    }
    in.close();
    proc.waitFor();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    return arr;
    }

    public static void main(String[] args) throws Exception {
    List<Double> list_one = new ArrayList<>();
    list_one.add(1.222);
    list_one.add(2.888);
    list_one.add(3.888);
    list_one.add(4.888);
    list_one.add(5.888);
    List<Integer> list_two = new ArrayList<>();
    list_two.add(1);
    list_two.add(2);
    list_two.add(3);
    list_two.add(4);
    list_two.add(5);
    String flag = Mogun.coalType_list(list_one, list_two);
    System.out.println(flag);

    }
    }
  • py文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    import numpy as np
    import sys as ss

    def StartStopOpt(turn,current):
    ............
    ...........
    return result

    def main():
    #初始数据
    # print (ss.argv[1:])
    list_str = []
    turn = []
    current = []
    for i in range(1, len(ss.argv)):
    list_str.append(ss.argv[i].replace(",", ""))
    list_str[0] = list_str[0].replace("[", "")
    list_str[len(ss.argv) - 2] = list_str[len(ss.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)

    # 调用函数传参得到返回值输出
    test = StartStopOpt(turn,current)
    print(test)


    if __name__ == '__main__':
    main()
  • 结果

    1
    2
    3
    4
    5
    6
    7
    8
    初始数据
    ['[1.222,', '2.888,', '3.888,', '4.888,', '5.888][1,', '2,', '3,', '4,', '5]']
    输出第一个数组
    [1, 2, 3, 4, 5]
    输出第二个数组
    [1.222, 2.888, 3.888, 4.888, 5.888]
    调用函数传参得到返回值输出
    ...省略

问题:数组的长度也是有限制的,我试着添加了8000元素的double类型的集合,就报错了
解决:https://www.cnblogs.com/downmoon/archive/2012/11/18/2775587.html

------  本文到此结束感谢您的阅读  ------
请“Chen77”喝杯咖啡
0%
返回顶部小火箭
隐藏
显示