3

There is a np.array:

[
array(['x_0', '2/20/1990', '3/20/1990'], dtype=object), 
array(['x_1', '1', '2'], dtype=object), 
array(['x_3', 'foo', 'bar'], dtype=object), etc...]

I want to make an array that will contain all of this values grouped (all first values with 1-st values, second values with second, and separated with "\n":

like this:

[array(['x_0\nx_1\nx_n, '2/20/1990\n1\nfoo', '3/20/1990\n2\bar', etc], dtype=object)]

2 Answers 2

2

You can use python built-in zip function and join :

>>> a=[
... np.array(['x_0', '2/20/1990', '3/20/1990'], dtype=object), 
... np.array(['x_1', '1', '2'], dtype=object), 
... np.array(['x_3', 'foo', 'bar'], dtype=object)]

>>> new=np.array(['\n'.join(k) for k in zip(*a)],dtype=object)
>>> new
array(['x_0\nx_1\nx_3', '2/20/1990\n1\nfoo', '3/20/1990\n2\nbar'], dtype=object)
Sign up to request clarification or add additional context in comments.

Comments

1

First transpose your matrix, and then concatenate values of each row using '\n' as a separator.

map(lambda x: '\n'.join(x), a.T)

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.