5

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?

2 Answers 2

11

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])
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the tuple(list) option since I didn't want to specify T and it works. Never knew that tuples can be assigned to arrays like that. Thanks.
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.