Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a Iron Python list to .NET array

I have a list comprehension operating on elements of an .NET array like

obj.arr = [f(x) for x in obj.arr]

However the assignment back to obj.arr fails.

Is it possible to convert a list to a .NET array in IronPython?

like image 444
Pradeep Avatar asked Dec 17 '22 07:12

Pradeep


2 Answers

Try this:

obj.arr = Array[T]([f(x) for x in obj.arr])

replacing T with type of array elements.

Alternatively:

obj.arr = tuple([f(x) for x in obj.arr])
like image 137
Pavel Minaev Avatar answered Dec 26 '22 16:12

Pavel Minaev


Arrays have to be typed as far as I know. This works for me:

num_list = [n for n in range(10)]

from System import Array
num_arr = Array[int](num_list)

Similarly for strings and other types.

like image 37
ars Avatar answered Dec 26 '22 17:12

ars