Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NET初始化一个锯齿状数组_Python_.net_Jagged Arrays_Python.net - Fatal编程技术网

Python NET初始化一个锯齿状数组

Python NET初始化一个锯齿状数组,python,.net,jagged-arrays,python.net,Python,.net,Jagged Arrays,Python.net,我想使用pythonnet包从Python初始化一个.NET锯齿数组。对于一维数组,我可以这样做: import clr from System import Array a = Array[int]([1, 2, 3]) int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }}; b = Array[Array[int]]([Array[int]([1,2,3]), Array[int]([4,5,6])

我想使用pythonnet包从Python初始化一个.NET锯齿数组。对于一维数组,我可以这样做:

import clr
from System import Array
a = Array[int]([1, 2, 3])
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }};
b = Array[Array[int]]([Array[int]([1,2,3]), Array[int]([4,5,6])])
但对于锯齿阵列,我如何才能做到这一点呢?因此,让我们假设我在python中使用了以下列表:

[[1, 2, 3], [4, 5, 6]]
在C#中,我会这样做:

import clr
from System import Array
a = Array[int]([1, 2, 3])
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }};
b = Array[Array[int]]([Array[int]([1,2,3]), Array[int]([4,5,6])])

在Python中,可以这样做:

import clr
from System import Array
a = Array[int]([1, 2, 3])
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }};
b = Array[Array[int]]([Array[int]([1,2,3]), Array[int]([4,5,6])])
或者,如果定义了辅助函数:

def asnetarray(x, defaulttype):
    if type(x) is list:
        if any([type(xi) is list for xi in x]):
            # Array of array
            return asnetarray([asnetarray(xi, defaulttype) for xi in x], defaulttype)
        elif x:
            # Array
            return Array[type(x[1])](x)
        else:
            # Empty array
            return Array[defaulttype]([])
    else:
        # Single element
        return Array[type(x)]([x])
然后可以将其用作:

# int[][]
b = asnetarray([[1, 2], [3, 4]], int)
# int[][][]
c = asnetarray([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], int)

从*Nix land这里进行疯狂猜测:
Array[int][1,2,3],[4,5,6])
relevant:所以试试看:
Array[int][Array[int][1,2,3]((Array[int][1,2,3)),(Array[int][4,5,6])
@juanpa.arrivillaga
b=Array[int][Array[int][1,2,3]),Array[int][4,5,6]))
有效。但如何编写python函数来转换具有n维的交错数组呢?