ToArray: Array;
The ToArray method transforms list contents into a Fore array.
The obtained array will have the Variant data type. If a list was multi-level one, the obtained array will also be multidimensional.
Executing the example requires a folder with Python modules: D:\Work\Python\. The folder contains the sample.py module. The following function is implemented in the module:
def getArr2Dim():
arr2Dim = [[1,2,3],[10,20,30]]
return arr2Dim
Add a link to the Python system assembly.
Sub UserProc;
Var
pUtils: IPythonUtils;
pList: IPythonList;
Result: Variant;
Arr: Array;
i, j: Integer;
Begin
pUtils := New PythonUtils.Create;
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Get list that is result of function execution
Result := pUtils.Invoke("sample", "getArr2Dim");
//Create a list from obtained result of the getArr2Dim function
pList := New PythonList.Create(Result);
//As the result of the getNewList function is an object, the created list in pList
//will consist of one element. This element should also be cast to the IPythonList type to
//work with single list elements from the getArr2Dim result.
pList := pList.Item(0) As IPythonList;
Arr := pList.ToArray;
Debug.WriteLine("Number of dimensions: " + Arr.Rank.ToString);
If Arr.Rank = 2 Then
For i := Arr.GetLowerBound(1) To Arr.GetUpperBound(1) Do
For j := Arr.GetLowerBound(2) To Arr.GetUpperBound(2) Do
Debug.Write(Arr[i, j] + ' ');
End For;
Debug.WriteLine("");
End For;
End If;
End Sub UserProc;
After executing the example the getArr2Dim Python function will be executed. This function returns a flat list. The obtained list will be converted to a flat Fore array. Values of array elements are displayed in the development environment console.
See also: