0

I am trying to figure out how to make columns out of arrays within an array. I am trying to save these columns to a text file.

I have tried multiple things, but I don't get columns. I get a large array that is going vertically down.

   a = [[array([ 2.05096059,  3.65564871,  0.25845727,  2.86561982,  1.45278606]), 
    array([5,10,15,20,25)]), 
    array([10,11,12,13,14])]

This is what I have tried, but it doesn't work.

for column in np.transpose(C):
    C_arr = np.hstack(column)
    # j = int(k)-1
    C_Values[j].append(C_arr)

I expect that I should get five columns with three elements in each column.

Expected output:

2.05096059 3.65564871 0.25845727 2.86561982 1.45278606
5          10         15         20         25
10         11         12         13         14
6
  • Be careful with terminology. a as shown looks like a list (object dtype) containing 3 arrays (ndarray). np.transpose(C) will turn that into a (3,5) array (float dtype) and then (5,3). Each column will be a (5,) shape array. What's wrong with C_Values? Commented Jan 11, 2019 at 21:51
  • Could you add the expected output? Commented Jan 11, 2019 at 21:51
  • Can't you just print each array separately, with its own format string (float vs integer)? It would a lot easier than trying mix them in one array (which normally uses a common dtype). Commented Jan 11, 2019 at 22:02
  • Yes, I will try that. Thanks! Commented Jan 11, 2019 at 22:20
  • C_Values is not doing the right number of columns and there are too many elements in each column when I use the code in my question. Commented Jan 11, 2019 at 22:24

1 Answer 1

1

After cleaning up mismatch :

In [373]: a =[np.array([ 2.05096059,  3.65564871,  0.25845727,  2.86561982,  1.4
     ...: 5278606]), 
     ...:     np.array([5,10,15,20,25]), 
     ...:     np.array([10,11,12,13,14])]
     ...:     
In [374]: a
Out[374]: 
[array([2.05096059, 3.65564871, 0.25845727, 2.86561982, 1.45278606]),
 array([ 5, 10, 15, 20, 25]),
 array([10, 11, 12, 13, 14])]

In [377]: np.transpose(a)  
Out[377]: 
array([[ 2.05096059,  5.        , 10.        ],
       [ 3.65564871, 10.        , 11.        ],
       [ 0.25845727, 15.        , 12.        ],
       [ 2.86561982, 20.        , 13.        ],
       [ 1.45278606, 25.        , 14.        ]])

This is a (5,3) array of floats; plain iteration produces the rows, one by one.

Printing each array individually lets us control format:

In [378]: print(('%10f'*5)%tuple(a[0]))
     ...: print(('%10d'*5)%tuple(a[1]))
     ...: print(('%10d'*5)%tuple(a[2]))
     ...: 
     ...: 
  2.050961  3.655649  0.258457  2.865620  1.452786
         5        10        15        20        25
        10        11        12        13        14

Using the standard numpy csv writter:

In [379]: np.savetxt('test.txt', a, fmt='%10f')
In [380]: cat test.txt
  2.050961   3.655649   0.258457   2.865620   1.452786
  5.000000  10.000000  15.000000  20.000000  25.000000
 10.000000  11.000000  12.000000  13.000000  14.000000

It's possible to set a different format for each column, but not for each row.

Sign up to request clarification or add additional context in comments.

1 Comment

This works! I still have gaps in my Python understanding, so thanks for the help!

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.